// app-store.ts
import { createStore } from "@stencil/store";
const DEFAULT_MODE = "prod";
const normalizeParentScope = (scope) => {
    const normalized = (scope || "").toLowerCase();
    if (normalized.includes("bot"))
        return "bot";
    if (normalized.includes("cta"))
        return "cta";
    if (normalized.includes("search"))
        return "search";
    return "unknown";
};
export const appStore = createStore({
    dataKey: "",
    dataMode: "prod",
    activeParentScope: "unknown",
    parentContexts: {
        bot: { dataKey: "", dataMode: DEFAULT_MODE },
        cta: { dataKey: "", dataMode: DEFAULT_MODE },
        search: { dataKey: "", dataMode: DEFAULT_MODE },
        unknown: { dataKey: "", dataMode: DEFAULT_MODE },
    },
});
export const setActiveParentComponent = (scope) => {
    appStore.state.activeParentScope = normalizeParentScope(scope);
};
export const setParentComponentState = (scope, dataKey, dataMode) => {
    const resolvedScope = normalizeParentScope(scope);
    const nextState = {
        dataKey: dataKey || "",
        dataMode: dataMode || DEFAULT_MODE,
    };
    appStore.state.parentContexts = Object.assign(Object.assign({}, appStore.state.parentContexts), { [resolvedScope]: nextState });
    appStore.state.dataKey = nextState.dataKey;
    appStore.state.dataMode = nextState.dataMode;
};
export const getParentComponentState = (scope) => {
    const resolvedScope = scope ? normalizeParentScope(scope) : appStore.state.activeParentScope;
    const scopedState = appStore.state.parentContexts[resolvedScope];
    if (scopedState === null || scopedState === void 0 ? void 0 : scopedState.dataKey) {
        return scopedState;
    }
    return {
        dataKey: appStore.state.dataKey || "",
        dataMode: appStore.state.dataMode || DEFAULT_MODE,
    };
};
