// /AIWK_COMMON/core/yw-window-state.js
// AIWK EXT4 v037
(function () {
    "use strict";

    const AIWK = window.AIWK;
    if (!AIWK) {
        throw new Error("yw-window-state stopped: AIWK is missing. 해결: aiwk_core.js를 먼저 로드하십시오.");
    }

    const DEFAULTS = {
        group: "home",
        net: "home",
        room: "prj_default",
        project_id: "default",
        stage1_id: "st1_control",
        stage2_id: "st2_default",
        singleton: 1,
        visible: 1,
        fold_level: 0
    };

    function moduleDefaults(moduleName) {
        const m = String(moduleName || "").toLowerCase();
        if (m.indexOf("wss") >= 0 || m.indexOf("bus") >= 0 || m.indexOf("nr_") >= 0 || m.indexOf("comm") >= 0) {
            return { stage1_id: "st1_control", stage2_id: "st2_comm", singleton: 1 };
        }
        if (m.indexOf("chart") >= 0 || m.indexOf("apex") >= 0 || m.indexOf("d3") >= 0) {
            return { stage1_id: "st1_chart", stage2_id: m.indexOf("d3") >= 0 ? "st2_d3" : "st2_apex", singleton: 0 };
        }
        if (m.indexOf("insta") >= 0 || m.indexOf("tiktok") >= 0 || m.indexOf("sns") >= 0) {
            return { stage1_id: "st1_sns", stage2_id: m.indexOf("tiktok") >= 0 ? "st2_tiktok" : "st2_instagram", singleton: 0 };
        }
        return {};
    }

    function normalize(cfg) {
        cfg = cfg || {};
        const profile = AIWK.store ? AIWK.store.get("profile", {}) : {};
        const byModule = moduleDefaults(cfg.module || cfg.moduleName || "");
        const id = String(cfg.id || cfg.win_id || "").trim();
        const out = Object.assign({}, DEFAULTS, byModule, cfg);
        out.win_id = id || out.win_id || "win_unknown";
        out.client_id = String((profile && profile.clientId) || cfg.client_id || cfg.clientId || "C1").trim() || "C1";
        out.group = String((profile && (profile.group || profile.net)) || cfg.group || cfg.net || out.group || out.net || "home").trim() || "home";
        out.net = out.group;
        out.room = String((profile && profile.room) || cfg.room || out.room || "prj_default").trim() || "prj_default";
        out.project_id = String(cfg.project_id || cfg.project || out.project_id || "default").trim() || "default";
        out.stage1_id = String(cfg.stage1_id || cfg.stage1 || out.stage1_id || "st1_control").trim() || "st1_control";
        out.stage2_id = String(cfg.stage2_id || cfg.stage2 || out.stage2_id || "st2_default").trim() || "st2_default";
        out.fold_level = Number(cfg.fold_level !== undefined ? cfg.fold_level : (cfg.collapseLevel !== undefined ? cfg.collapseLevel : out.fold_level)) || 0;
        out.visible = cfg.visible === 0 || cfg.hidden ? 0 : 1;
        out.singleton = Number(cfg.singleton !== undefined ? cfg.singleton : out.singleton) ? 1 : 0;
        out.route = "/" + [out.project_id, out.stage1_id, out.stage2_id, out.win_id, "state"].join("/");
        out.folder_route = out.route;
        out.address = AIWK.route && AIWK.route.buildAddress ? AIWK.route.buildAddress({ group: out.group, room: out.room, client_id: out.client_id, endpoint: "state", project_id: out.project_id, stage1_id: out.stage1_id, stage2_id: out.stage2_id, win_id: out.win_id, port: "state" }) : "";
        return out;
    }

    AIWK.windowState = {
        defaults: DEFAULTS,
        normalize: normalize,
        moduleDefaults: moduleDefaults
    };
}());
