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KAAY,QACjB,QAAQ,KAAK,2BAA6B,EAAS,iDAAiD,EAExG,EAAM,GAAU,EAAO,EAC3B,CAAC,EAEE,EAAM,YAAc,QACnB,QAAQ,KAAK,+EAAgF,EAEjG,EAAM,UAAY,EAAO,UAElB,CACX,EACI,EAAQ,UAAY,QAEpB,GAAQ,QAAU,CAAC,GAGhB,CAEH,aAAc,EACd,QACJ,CACJ","sourcesContent":["// @ts-ignore\nimport type { _GettersTree, DefineStoreOptions, StateTree, Store, _StoreWithState } from \"pinia\";\n// @ts-ignore\nimport { defineStore } from 'pinia';\nimport { computed, reactive, Ref, watch } from \"vue\";\nimport { doubleTypes } from \"../../dev-types\";\nimport { getApiMap, useDouble } from \"../double/useDouble\";\n\nconst stores: Record<keyof doubleTypes, any> = {}\n\nexport function defineDoublePiniaStore<\n    Path extends keyof doubleTypes,\n    Id extends string,\n    State extends StateTree = {},\n    Getters extends _GettersTree<State> = {},\n    Actions = {}\n>(path: Path, options: Omit<DefineStoreOptions<Id, State, Getters, Actions>, 'id'>): () => Promise<Store<\n    Id,\n    State & doubleTypes[Path]['state'] & { isLoading: doubleTypes[Path]['isLoading'] },\n    Getters,\n    Actions & { refresh: () => Promise<void> } & doubleTypes[Path]['actions']\n>> {\n    return () => {\n        return new Promise((resolve, reject) => {\n            console.log(stores)\n            if(stores[path] !== undefined) {\n                resolve(stores[path])\n                return\n            }\n            injectDouble(path, options).then(({storeOptions, config}) => {\n                // @ts-ignore\n                const id: Id = path\n                const store = defineStore(id, storeOptions)()\n\n                // Setup a watcher on the queryConfig getter\n                // So we can tell double to re-fetch the data whenever it changes\n                if(typeof store.queryConfig !== undefined) {\n                    watch(computed(() => store.queryConfig), () => {\n                        Object.entries(store.queryConfig).forEach(([key, value]) => {\n                            config[key] = value\n                        })\n                    })\n                }\n                stores[path] = store\n                resolve(store)\n            }).catch((e) => {\n                throw e\n            })\n        })\n        \n    }\n}\n\nexport async function injectDouble<\n    Path extends keyof doubleTypes,\n    Id extends string,\n    State extends StateTree = {},\n    Getters extends _GettersTree<State> = {},\n    Actions = {}\n>(path: Path, options: Omit<DefineStoreOptions<Id, State, Getters, Actions>, 'id'>):\n    Promise<{\n        storeOptions: Omit<DefineStoreOptions<\n            Id,\n            State & doubleTypes[Path]['state'] & { isLoading: doubleTypes[Path]['isLoading'] },\n            Getters,\n            Actions & { refresh: () => Promise<void> } & doubleTypes[Path]['actions']\n        >, 'id'>,\n        config: {}\n    }> {\n\n    const originalState = options.state\n\n    // Todo: This might not be the best idea, as options.state is a function which returns a unique state object and all of those\n    // state objects will use the same `double` store\n    const config = reactive({})\n    const double = await useDouble(path, config)\n    const apiMap = await getApiMap(path)\n    \n    // TODO: Properly fix @ts-ignore stuff\n\n    // @ts-ignore\n    options.state = () => {\n        // @ts-ignore\n        if(options.actions.refresh !== undefined) {\n            console.warn('Overriding the refresh action is currently not supported. You can create a customRefresh action which calls double\\'s refresh action.')\n        }\n        // @ts-ignore\n        options.actions.refresh = function () {\n            double.refresh()\n        }\n\n        apiMap.actions.forEach((action) => {\n            if(options.actions[action] !== undefined) {\n                console.warn('You can not specify the ' + action + ' action if your PHP file already defines it. You can create your own custom action which calls this action.')\n            }\n            options.actions[action] = double[action]\n        })\n        let state: Record<string, any> = {}\n        if (originalState) {\n            state = originalState()\n        }\n        // In this context \"getter\" means a double getter (e.g. getBlogEntries) and not a pinia getter.\n        apiMap.getters.forEach((getter) => {\n            if(state[getter] !== undefined) {\n                console.warn('You can not specify the ' + getter + ' state key if your PHP file already defines it.')\n            }\n            state[getter] = double[getter]\n        })\n\n        if(state.isLoading !== undefined) {\n            console.warn('You can not specify the isLoading state key, because it\\'s reserved by double.')\n        }\n        state.isLoading = double.isLoading\n\n        return state\n    }\n    if (options.actions === undefined) {\n        // @ts-ignore\n        options.actions = {}\n    }\n\n    return {\n        // @ts-ignore\n        storeOptions: options,\n        config,\n    }\n}\n"]}