"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    createAppLoadedListener: ()=>createAppLoadedListener,
    createBaseStore: ()=>createBaseStore,
    getFacetElements: ()=>getFacetElements,
    isRefineModalFacet: ()=>isRefineModalFacet,
    registerFacet: ()=>registerFacet,
    setLoadingFlag: ()=>setLoadingFlag,
    unsetLoadingFlag: ()=>unsetLoadingFlag,
    waitUntilAppLoaded: ()=>waitUntilAppLoaded
});
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const isRefineModalFacet = 'is-refine-modal';
function createAppLoadedListener(store, callback) {
    const updateIsAppLoaded = ()=>{
        const isAppLoaded = 0 === store.state.loadingFlags.length;
        callback(isAppLoaded);
    };
    store.onChange('loadingFlags', updateIsAppLoaded);
    updateIsAppLoaded();
}
function createBaseStore(initialState) {
    const store = createStore(initialState);
    return {
        ...store,
        getUniqueIDFromEngine (_engine) {
            throw new Error('getUniqueIDFromEngine not implemented at the base store level.');
        }
    };
}
function registerFacet(store, facetType, data) {
    const clearExistingFacetElement = (facetType, facetId)=>{
        if (store.state[facetType][facetId]) store.state.facetElements = store.state.facetElements.filter((facetElement)=>facetElement.getAttribute('facet-id') !== facetId);
    };
    if (null !== data.element.getAttribute(isRefineModalFacet)) return;
    clearExistingFacetElement(facetType, data.facetId);
    store.state.facetElements.push(data.element);
    store.state[facetType][data.facetId] = data;
}
function setLoadingFlag(store, loadingFlag) {
    const flags = store.state.loadingFlags;
    store.state.loadingFlags = flags.concat(loadingFlag);
}
function unsetLoadingFlag(store, loadingFlag) {
    const flags = store.state.loadingFlags;
    store.state.loadingFlags = flags.filter((value)=>value !== loadingFlag);
}
function getFacetElements(store) {
    return store.state.facetElements.filter((element)=>(0, utils_cjs_namespaceObject.isInDocument)(element));
}
function waitUntilAppLoaded(store, callback) {
    if (store.state.loadingFlags.length) store.onChange('loadingFlags', (flags)=>{
        if (!flags.length) callback();
    });
    else callback();
}
function createStore(initialState) {
    const listeners = new Map();
    const state = new Proxy(initialState, {
        set (target, prop, value) {
            const oldValue = target[prop];
            if (oldValue !== value) {
                target[prop] = value;
                if (listeners.has(prop)) for (const cb of listeners.get(prop))cb(value);
            }
            return true;
        }
    });
    const get = (propName)=>state[propName];
    const set = (propName, value)=>{
        state[propName] = value;
    };
    const onChange = (propName, callback)=>{
        if (!listeners.has(propName)) listeners.set(propName, new Set());
        listeners.get(propName).add(callback);
        return ()=>{
            listeners.get(propName).delete(callback);
            if (0 === listeners.get(propName).size) listeners.delete(propName);
        };
    };
    return {
        state,
        get,
        set,
        onChange
    };
}
exports.createAppLoadedListener = __webpack_exports__.createAppLoadedListener;
exports.createBaseStore = __webpack_exports__.createBaseStore;
exports.getFacetElements = __webpack_exports__.getFacetElements;
exports.isRefineModalFacet = __webpack_exports__.isRefineModalFacet;
exports.registerFacet = __webpack_exports__.registerFacet;
exports.setLoadingFlag = __webpack_exports__.setLoadingFlag;
exports.unsetLoadingFlag = __webpack_exports__.unsetLoadingFlag;
exports.waitUntilAppLoaded = __webpack_exports__.waitUntilAppLoaded;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "createAppLoadedListener",
    "createBaseStore",
    "getFacetElements",
    "isRefineModalFacet",
    "registerFacet",
    "setLoadingFlag",
    "unsetLoadingFlag",
    "waitUntilAppLoaded"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
