import { html } from "../util.js";
import buttonCSS from "../buttonStyles.js";
const SORT_OPTIONS = [
{ value: "newest", label: "Newest" },
{ value: "oldest", label: "Oldest" },
{ value: "name", label: "Name" },
];
class PxmeToolbar extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this._sortIndex = 0;
}
static get observedAttributes() {
return ["sort"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "sort" && this._rendered) {
this._sortIndex = SORT_OPTIONS.findIndex((o) => o.value === newValue);
if (this._sortIndex < 0) this._sortIndex = 0;
this._updateSortTitle();
}
}
connectedCallback() {
const initialSort = this.getAttribute("sort") || "newest";
this._sortIndex = SORT_OPTIONS.findIndex((o) => o.value === initialSort);
if (this._sortIndex < 0) this._sortIndex = 0;
this.shadowRoot.innerHTML = html`
`;
this._rendered = true;
const sortBtn = this.shadowRoot.querySelector("#sort-btn");
sortBtn.addEventListener("click", () => {
this._sortIndex = (this._sortIndex + 1) % SORT_OPTIONS.length;
this._updateSortTitle();
this.dispatchEvent(
new CustomEvent("sort-change", {
detail: { sort: SORT_OPTIONS[this._sortIndex].value },
bubbles: true,
composed: true,
})
);
});
}
_updateSortTitle() {
const btn = this.shadowRoot.querySelector("#sort-btn");
if (btn) {
btn.setAttribute("aria-label", `Sort by ${SORT_OPTIONS[this._sortIndex].label}`);
}
}
get sort() {
return SORT_OPTIONS[this._sortIndex].value;
}
}
customElements.define("pxme-toolbar", PxmeToolbar);