var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _EdenMainUi_edit, _EdenMainUi_info, _EdenMainUi_save, _EdenMainUi_cancel, _EdenMainUi_logout, _EdenMainUi_editNav, _EdenMainUi_toggleInfo, _EdenMainUi_keyDown, _EdenMainUi_lifecycleHandlers, _EdenMainUi_openPreview, _EdenMainUi_beforeUnload, _EdenMainUi_refreshSite, _EdenMainUi_openFileManager, _EdenMainUi_duplicatePage, _EdenMainUi_showDuplicationModal, _EdenMainUi_updateUpdatedByTooltip, _EdenMainUi_playerInitials, _EdenMainUi_renderPlayers, _EdenMainUi_playersUpdated;
import { injectStyles } from "../../services/styles.js";
import edenStyles from "../styles/eden-styles.js";
import { initMainUiShadow, resolveMainUiFlags, } from "./eden-main-ui/bootstrap.js";
import { cancelMainUi, editMainUi, infoMainUi, logoutMainUi, saveMainUi, startMainUiLoading, stopMainUiLoading, toggleMainUiActive, toggleMainUiInfo, } from "./eden-main-ui/mode-actions.js";
import { duplicatePage, openEditStructureModal, openFileManagerModal, openMainPreview, refreshMainSite, showPageDuplicationModal, updateUpdatedByTooltip, } from "./eden-main-ui/integrations.js";
import { handleMainUiBeforeUnload, handleMainUiKeyDown, } from "./eden-main-ui/shortcuts.js";
import { bindMainUiElements, connectMainUiListeners, disconnectMainUiListeners, } from "./eden-main-ui/lifecycle.js";
import { createMainUiLifecycleHandlers, createMainUiShortcutContext, } from "./eden-main-ui/wiring.js";
import { EdenEvents } from "../../services/event.js";
import { generateColor } from "../../services/colors.js";
class EdenMainUi extends HTMLElement {
    constructor() {
        super();
        this.startLoading = () => {
            startMainUiLoading(this.saveButton);
        };
        this.stopLoading = () => {
            stopMainUiLoading(this.saveButton);
        };
        _EdenMainUi_edit.set(this, () => {
            editMainUi(this, this.toggleActive);
        });
        _EdenMainUi_info.set(this, () => {
            infoMainUi(this, __classPrivateFieldGet(this, _EdenMainUi_toggleInfo, "f"));
        });
        _EdenMainUi_save.set(this, async () => {
            await saveMainUi(this, __classPrivateFieldGet(this, _EdenMainUi_updateUpdatedByTooltip, "f"));
        });
        _EdenMainUi_cancel.set(this, () => {
            cancelMainUi(this, this.toggleActive);
        });
        _EdenMainUi_logout.set(this, () => {
            logoutMainUi(this);
        });
        _EdenMainUi_editNav.set(this, async () => {
            await openEditStructureModal();
        });
        this.toggleActive = () => {
            toggleMainUiActive(this);
        };
        _EdenMainUi_toggleInfo.set(this, () => {
            toggleMainUiInfo(this);
        });
        _EdenMainUi_keyDown.set(this, (e) => {
            const shortcutsContext = createMainUiShortcutContext(this, {
                save: __classPrivateFieldGet(this, _EdenMainUi_save, "f"),
                openFileManager: __classPrivateFieldGet(this, _EdenMainUi_openFileManager, "f"),
                info: __classPrivateFieldGet(this, _EdenMainUi_info, "f"),
                editNav: __classPrivateFieldGet(this, _EdenMainUi_editNav, "f"),
                logout: __classPrivateFieldGet(this, _EdenMainUi_logout, "f"),
                cancel: __classPrivateFieldGet(this, _EdenMainUi_cancel, "f"),
                edit: __classPrivateFieldGet(this, _EdenMainUi_edit, "f"),
                openPreview: __classPrivateFieldGet(this, _EdenMainUi_openPreview, "f"),
            });
            handleMainUiKeyDown(shortcutsContext, e);
        });
        _EdenMainUi_lifecycleHandlers.set(this, () => {
            return createMainUiLifecycleHandlers({
                openFileManager: __classPrivateFieldGet(this, _EdenMainUi_openFileManager, "f"),
                editNav: __classPrivateFieldGet(this, _EdenMainUi_editNav, "f"),
                refreshSite: __classPrivateFieldGet(this, _EdenMainUi_refreshSite, "f"),
                showDuplicationModal: __classPrivateFieldGet(this, _EdenMainUi_showDuplicationModal, "f"),
                openPreview: __classPrivateFieldGet(this, _EdenMainUi_openPreview, "f"),
                info: __classPrivateFieldGet(this, _EdenMainUi_info, "f"),
                edit: __classPrivateFieldGet(this, _EdenMainUi_edit, "f"),
                save: __classPrivateFieldGet(this, _EdenMainUi_save, "f"),
                cancel: __classPrivateFieldGet(this, _EdenMainUi_cancel, "f"),
                logout: __classPrivateFieldGet(this, _EdenMainUi_logout, "f"),
                keyDown: __classPrivateFieldGet(this, _EdenMainUi_keyDown, "f"),
                beforeUnload: __classPrivateFieldGet(this, _EdenMainUi_beforeUnload, "f"),
            });
        });
        _EdenMainUi_openPreview.set(this, () => {
            openMainPreview();
        });
        _EdenMainUi_beforeUnload.set(this, (e) => {
            handleMainUiBeforeUnload(this.unsavedChanges, e);
        });
        _EdenMainUi_refreshSite.set(this, async () => {
            await refreshMainSite();
        });
        _EdenMainUi_openFileManager.set(this, async () => {
            await openFileManagerModal();
        });
        _EdenMainUi_duplicatePage.set(this, async (lang, path, ignoreFixedEditors) => {
            await duplicatePage(lang, path, ignoreFixedEditors);
        });
        _EdenMainUi_showDuplicationModal.set(this, async () => {
            await showPageDuplicationModal(__classPrivateFieldGet(this, _EdenMainUi_duplicatePage, "f"));
        });
        _EdenMainUi_updateUpdatedByTooltip.set(this, async () => {
            await updateUpdatedByTooltip(this);
        });
        _EdenMainUi_playerInitials.set(this, (player) => {
            return `${player.firstName.charAt(0)}${player.lastName.charAt(0)}`.toUpperCase();
        });
        _EdenMainUi_renderPlayers.set(this, (players) => {
            this.playersContainer.replaceChildren();
            players.forEach((player) => {
                const initials = __classPrivateFieldGet(this, _EdenMainUi_playerInitials, "f").call(this, player);
                const color = generateColor(initials);
                const avatar = document.createElement("div");
                avatar.setAttribute("class", "player");
                avatar.setAttribute("title", `${player.firstName} ${player.lastName}`);
                avatar.style.setProperty("--player-color", `var(${color})`);
                avatar.textContent = initials;
                this.playersContainer.appendChild(avatar);
            });
        });
        _EdenMainUi_playersUpdated.set(this, (event) => {
            const customEvent = event;
            const players = Array.isArray(customEvent.detail?.players)
                ? customEvent.detail.players
                : [];
            __classPrivateFieldGet(this, _EdenMainUi_renderPlayers, "f").call(this, players);
        });
        this.user = window.Eden.user;
        this.lastUpdated = undefined;
        this.lastUpdatedBy = undefined;
        const flags = resolveMainUiFlags(this.user);
        this.isEditor = flags.isEditor;
        this.isAdmin = flags.isAdmin;
        this.isSuperAdmin = flags.isSuperAdmin;
        this.unsavedChanges = false;
        initMainUiShadow(this, {
            firstName: this.user.firstName,
            isEditor: this.isEditor,
            isAdmin: this.isAdmin,
            lastUpdated: this.lastUpdated,
            lastUpdatedBy: this.lastUpdatedBy,
        });
    }
    static get observedAttributes() {
        return ["active", "info"];
    }
    get active() {
        return this.hasAttribute("active");
    }
    set active(value) {
        if (value) {
            this.setAttribute("active", "");
        }
        else {
            this.removeAttribute("active");
        }
    }
    get info() {
        return this.hasAttribute("info");
    }
    set info(value) {
        if (value) {
            this.setAttribute("info", "");
        }
        else {
            this.removeAttribute("info");
        }
    }
    connectedCallback() {
        __classPrivateFieldGet(this, _EdenMainUi_updateUpdatedByTooltip, "f").call(this);
        bindMainUiElements(this);
        const currentPlayers = Array.isArray(window.Eden.players)
            ? window.Eden.players
            : [];
        __classPrivateFieldGet(this, _EdenMainUi_renderPlayers, "f").call(this, currentPlayers);
        const handlers = __classPrivateFieldGet(this, _EdenMainUi_lifecycleHandlers, "f").call(this);
        connectMainUiListeners(this, handlers, this.isAdmin);
        document.addEventListener(EdenEvents.playersUpdated, __classPrivateFieldGet(this, _EdenMainUi_playersUpdated, "f"));
        injectStyles("eden-styles", [edenStyles]);
    }
    disconnectedCallback() {
        document.removeEventListener(EdenEvents.playersUpdated, __classPrivateFieldGet(this, _EdenMainUi_playersUpdated, "f"));
        const handlers = __classPrivateFieldGet(this, _EdenMainUi_lifecycleHandlers, "f").call(this);
        disconnectMainUiListeners(this, handlers, this.isAdmin);
    }
}
_EdenMainUi_edit = new WeakMap(), _EdenMainUi_info = new WeakMap(), _EdenMainUi_save = new WeakMap(), _EdenMainUi_cancel = new WeakMap(), _EdenMainUi_logout = new WeakMap(), _EdenMainUi_editNav = new WeakMap(), _EdenMainUi_toggleInfo = new WeakMap(), _EdenMainUi_keyDown = new WeakMap(), _EdenMainUi_lifecycleHandlers = new WeakMap(), _EdenMainUi_openPreview = new WeakMap(), _EdenMainUi_beforeUnload = new WeakMap(), _EdenMainUi_refreshSite = new WeakMap(), _EdenMainUi_openFileManager = new WeakMap(), _EdenMainUi_duplicatePage = new WeakMap(), _EdenMainUi_showDuplicationModal = new WeakMap(), _EdenMainUi_updateUpdatedByTooltip = new WeakMap(), _EdenMainUi_playerInitials = new WeakMap(), _EdenMainUi_renderPlayers = new WeakMap(), _EdenMainUi_playersUpdated = new WeakMap();
export default EdenMainUi;
if (!customElements.get("eden-main-ui")) {
    customElements.define("eden-main-ui", EdenMainUi);
}
