// /AIWK_COMMON/core/yw-module-install.js
// AIWK EXT4 v041
// 역할: 도메인 모듈 설치/사용 여부의 표준 상태 구조를 정의한다. 실제 저장은 EXT popup chrome.storage 또는 WEB localStorage가 담당한다.
(function () {
    "use strict";
    const AIWK = window.AIWK || (window.AIWK = {});

    function nowIso() { return new Date().toISOString(); }

    function makeModuleState(moduleRow, patch) {
        moduleRow = moduleRow || {};
        patch = patch || {};
        return Object.assign({
            module_id: moduleRow.module_id || "",
            module_key: moduleRow.module_key || "",
            dev_key: moduleRow.dev_key || "",
            feature_key: moduleRow.feature_key || "",
            representative_domain: moduleRow.representative_domain || "",
            service_or_group: moduleRow.service_or_group || "",
            installed: 0,
            enabled: 0,
            run_mode: "manual",
            auto_run: 0,
            source: "local",
            updated_at: nowIso()
        }, patch || {});
    }

    function applyChoice(settings, moduleRow, patch) {
        settings = settings || { modules: {} };
        if (!settings.modules) { settings.modules = {}; }
        const state = makeModuleState(moduleRow, patch || {});
        if (!state.module_id) {
            throw new Error("MODULE_ID_EMPTY");
        }
        settings.modules[state.module_id] = Object.assign({}, settings.modules[state.module_id] || {}, state, { updated_at: nowIso() });
        return settings;
    }

    AIWK.moduleInstall = {
        version: "v041",
        makeModuleState: makeModuleState,
        applyChoice: applyChoice
    };
}());
