import { html } from "lit"; import { customElement, query, state } from "lit/decorators.js"; import Countries from "resources/countries.json" with { type: "json" }; import { translateText } from "./Utils"; import { BaseModal } from "./components/BaseModal"; import { modalHeader } from "./components/ui/ModalHeader"; @customElement("flag-input-modal") export class FlagInputModal extends BaseModal { @query("#flag-input-modal") private modalRef!: HTMLElement; @state() private search = ""; public returnTo = ""; updated(changedProperties: Map) { super.updated(changedProperties); } render() { const content = html`
${modalHeader({ title: translateText("flag_input.title"), onBack: () => this.close(), ariaLabel: translateText("common.back"), })}
${Countries.filter( (country) => !country.restricted && this.includedInSearch(country), ).map( (country) => html` `, )}
`; if (this.inline) { return content; } return html` ${content} `; } private includedInSearch(country: { name: string; code: string }): boolean { return ( country.name.toLowerCase().includes(this.search.toLowerCase()) || country.code.toLowerCase().includes(this.search.toLowerCase()) ); } private handleSearch(event: Event) { this.search = (event.target as HTMLInputElement).value; } private setFlag(flag: string) { localStorage.setItem("flag", flag); this.dispatchEvent( new CustomEvent("flag-change", { detail: { flag }, bubbles: true, composed: true, }), ); } protected onOpen(): void { // No custom logic needed } protected onClose(): void { if (this.returnTo) { const returnEl = document.querySelector(this.returnTo) as any; if (returnEl?.open) { returnEl.open(); } this.returnTo = ""; } } }