import { html, LitElement, nothing } from "lit";
import { customElement, state, property } from "lit/decorators.js";
import { PublisherManager } from "@supersoniks/concorde/utils";
import { tailwind } from "../tailwind";
import search from "./docs-search.json";
import "@supersoniks/concorde/core/components/ui/divider/divider";
import "@supersoniks/concorde/core/components/ui/icon/icon";
import "@supersoniks/concorde/core/components/ui/menu/menu";
import "@supersoniks/concorde/core/components/ui/pop/pop";
import { customScroll } from "@supersoniks/concorde/core/components/ui/_css/scroll";
import { FormElementInterface } from "@supersoniks/concorde/core/mixins/FormElement";
import { Pop } from "@supersoniks/concorde/core/components/ui/pop/pop";
const tagName = "docs-search"; // For Astro.build
const win: any = window;
const fuzzysort = win.fuzzysort;
@customElement(tagName)
export class DocsSearch extends LitElement {
static styles = [tailwind, customScroll];
@state({ type: Boolean }) hidePopMenu = false;
@property({ type: Boolean }) setFocus = false;
search(val) {
// if (!val || val.length < 3) {
// this.hidePopMenu = true;
// return null;
// } else this.hidePopMenu = false;
PublisherManager.get("searchSelection").selection = null;
const searchResult = fuzzysort.go(val, search, { key: "search" });
const resultForList: Array = [];
const titlesToContent: Map = new Map();
const found: Array = [];
for (const result of searchResult) {
if (result.score < -100000) continue;
const obj = result.obj;
for (const file in obj.files) {
for (const part in obj.files[file].hashes) {
const hash = obj.files[file].hashes[part];
if (found.includes(file + ":" + part)) continue;
found.push(file + ":" + part);
const title = obj.files[file].title;
const subItem = {
href: "#" + file + "/" + part,
part: hash.title || "...",
search:
hash.type == "page" ? "Page" : this.getResultSearchString(result),
index: search.indexOf(obj),
template: hash.type == "page" ? "page" : null,
};
if (!titlesToContent.has(title)) {
const item: any = {
title: title,
items: [subItem],
};
titlesToContent.set(title, item);
resultForList.push(item);
} else {
const item = titlesToContent.get(title);
item.items.push(subItem);
}
}
}
}
(this.shadowRoot?.querySelector("sonic-pop") as Pop)?.show();
// this.hidePopMenu = resultForList.length == 0;
for (const item of resultForList) {
item.items.sort((a, b) => a.index - b.index);
}
PublisherManager.get("searchResult").set(resultForList);
}
getResultSearchString(result: any) {
const p = result.p;
const str = result.target
.split("")
.map((value, index) =>
p.includes(index)
? "" +
value +
""
: value
)
.join("");
return str;
}
connectedCallback() {
PublisherManager.get("docsSearch").search.onAssign((v) => this.search(v));
super.connectedCallback();
}
firstUpdated() {
if (this.setFocus) {
setTimeout(() => {
const input = this.shadowRoot?.querySelector(
"sonic-input"
) as FormElementInterface;
input?.focus();
(this.shadowRoot?.querySelector("sonic-pop") as Pop)?.show();
}, 100);
}
}
render() {
return html`
${this.hidePopMenu
? nothing
: html`
`}
`;
}
}