import { html } from "lit"; import { customElement, state } from "lit/decorators.js"; import Countries from "resources/countries.json" with { type: "json" }; import { UserMeResponse } from "src/core/ApiSchemas"; import { assetUrl } from "src/core/AssetUrls"; import { Cosmetics } from "src/core/CosmeticSchemas"; import { UserSettings } from "src/core/game/UserSettings"; import { getUserMe } from "./Api"; import { fetchCosmetics, flagRelationship } from "./Cosmetics"; import { translateText } from "./Utils"; import { BaseModal } from "./components/BaseModal"; import "./components/FlagButton"; import "./components/NotLoggedInWarning"; import { modalHeader } from "./components/ui/ModalHeader"; @customElement("flag-input-modal") export class FlagInputModal extends BaseModal { @state() private search = ""; @state() private cosmetics: Cosmetics | null = null; @state() private userMe: UserMeResponse | false = false; public returnTo = ""; updated(changedProperties: Map) { super.updated(changedProperties); } private renderFlags() { const userSettings = new UserSettings(); const selectedFlag = userSettings.getFlag() ?? ""; const onSelect = (flagKey: string) => { this.setFlag(flagKey); this.close(); }; const cosmeticFlags = Object.entries(this.cosmetics?.flags ?? {}) .filter(([, flag]) => { if (!this.includedInSearch({ name: flag.name, code: flag.name })) return false; return flagRelationship(flag, this.userMe, null) === "owned"; }) .map( ([key, flag]) => html` `, ); const noFlag = this.search ? null : html` `; const countryFlags = Countries.filter( (country) => country.code !== "xx" && !country.restricted && this.includedInSearch(country), ).map( (country) => html` `, ); return html` ${noFlag} ${cosmeticFlags} ${countryFlags} `; } render() { const content = html` ${modalHeader({ title: translateText("flag_input.title"), onBack: () => this.close(), ariaLabel: translateText("common.back"), rightContent: html``, })} { this.close(); window.showPage?.("page-item-store"); }} > ${translateText("main.store")} ${this.renderFlags()} `; 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) { new UserSettings().setFlag(flag); } protected async onOpen(): Promise { [this.cosmetics, this.userMe] = await Promise.all([ fetchCosmetics(), getUserMe().then((r) => r || (false as const)), ]); } protected onClose(): void { this.search = ""; if (this.returnTo) { const returnEl = document.querySelector(this.returnTo) as any; if (returnEl?.open) { returnEl.open(); } this.returnTo = ""; } } }