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,EAMM,CACH,GAAG,EACH,GAAG,EACH,YACA,QARY,SAAY,CACxB,EAAQ,KAAM,GAAsB,CAAC,CACzC,CAOA,CACJ,CAEA,iBAAgC,EAAiE,CAC7F,GAAI,GAAS,KACb,MAAG,GAAW,IAAM,UAGhB,GAAO,EAAK,QAAQ,YAAa,EAAE,EACnC,EAAU,MAAM,QAAkC,QAAU,EAAO,SAAS,SAE5E,EAAU,MAAM,QAA0B,EAAO,SAAS,QAE1D,GACA,QAAQ,MAAM,uBAAuB,6CAAgD,EAElF,CACX","sourcesContent":["// Inspired by https://github.com/DanHulton/vue-deepunref\n// Published on the MIT license\nimport { unref, isRef }  from 'vue'\n\nconst isObject = (val) => val !== null && typeof val === 'object';\nconst isArray = Array.isArray;\n\n/**\n * Deeply unref a value, recursing into objects and arrays.\n *\n * @param {Mixed} val - The value to deeply unref.\n *\n * @return {Mixed}\n */\nexport const deepUnref = (val) => {\n  const checkedVal = isRef(val) ? unref(val) : val;\n\n  if (! isObject(checkedVal)) {\n    return checkedVal;\n  }\n\n  if (isArray(checkedVal)) {\n    return unrefArray(checkedVal);\n  }\n\n  return unrefObject(checkedVal);\n};\n\n/**\n * Unref a value, recursing into it if it's an object.\n *\n * @param {Mixed} val - The value to unref.\n *\n * @return {Mixed}\n */\nconst smartUnref = (val) => {\n  // Non-ref object?  Go deeper!\n  if (val !== null && ! isRef(val) && typeof val === 'object') {\n    return deepUnref(val);\n  }\n\n  return unref(val);\n};\n\n/**\n * Unref an array, recursively.\n *\n * @param {Array} arr - The array to unref.\n *\n * @return {Array}\n */\nconst unrefArray = (arr) => arr.map(smartUnref);\n\n/**\n * Unref an object, recursively.\n *\n * @param {Object} obj - The object to unref.\n *\n * @return {Object}\n */\nconst unrefObject = (obj) => {\n  const unreffed = {};\n\n  // Object? un-ref it!\n  Object.keys(obj).forEach((key) => {\n    unreffed[key] = smartUnref(obj[key]);\n  });\n\n  return unreffed;\n}","import { doubleTypes } from \"../../dev-types\"\nimport { deepUnref } from \"./deepUnref\"\nlet backendPath = ''\n\n/**\n * Custom headers we sent with every requests\n * This will most probably be used for an auth header\n */\nlet customHeaders: Record<string, string> = {}\n\nconst assureBackendPath = () => {\n    if(!backendPath) {\n        throw new Error('No backend path is set. Did you call the double install function?')\n    }\n}\n\n/**\n * This sets the HTTP path pointing to the root double API in the Laravel project\n * By default this should be https://{yourdomain}.com/double\n * \n * @param path \n */\nexport const setBackendPath = (path: string): void => {\n    backendPath = path\n}\n\n/**\n * Sets the custom headers. This overrides all existing custom headers\n * @example { Cookie: 'myCookie=1', 'x-my-custom-header': 5 }\n * @param headers An object where the keys are the names of the header and the values are the values that should be set\n */\nexport const setCustomHeaders = (headers: Record<string, string>): void => {\n    customHeaders = headers\n}\n\n/**\n * Sets a single header that will be sent with every request\n * \n * @param header the name of the header\n * @param value the value that should be set for that header\n */\nexport const setCustomHeader = (header: string, value: string): void => {\n    customHeaders[header] = value\n}\n\nexport const getHeaders = (): Record<string, string> => {\n    const baseHeaders = {\n        'Content-Type': 'application/json'\n    }\n    return {\n        ...baseHeaders,\n        ...customHeaders,\n    }\n}\n\nexport const loadData = async <Path extends keyof doubleTypes>(path: Path, config: Record<string, unknown> = {}) => {\n    assureBackendPath()\n    const res = await fetch(`${backendPath}/data?path=${encodeURIComponent(path)}`, {\n        method: 'POST',\n        credentials: \"include\",\n        headers: getHeaders(),\n        body: JSON.stringify({ config: deepUnref(config) })\n    })\n    return await res.json()\n}\n\nexport const callAction = async <Path extends keyof doubleTypes>(path: Path, method: string, data: Record<string, unknown>)  => {\n    assureBackendPath()\n    const res = await fetch(`${backendPath}/action?path=${encodeURIComponent(path)}&method=${encodeURIComponent(method)}`, {\n        method: 'POST',\n        credentials: \"include\",\n        headers: getHeaders(),\n        body: JSON.stringify(deepUnref(data))\n    })\n    return await res.json()\n}","import {reactive, ref, watch, isRef } from \"vue\"\n\nimport { callAction, loadData } from \"./api\";\nimport { doubleTypes } from \"../../dev-types\";\nimport { getBundler } from \"./bundler\";\n\nexport async function useDouble<Path extends keyof doubleTypes>(path: Path, config: Record<string, any> = {}):\n    Promise<\n        doubleTypes[Path]['state'] &\n        doubleTypes[Path]['actions'] &\n        { isLoading: doubleTypes[Path]['isLoading'] } &\n        { refresh: () => Promise<void> }\n    > {\n    // To be able to watch the config it has to be a ref\n    if(!isRef(config)) {\n        config = ref(config)\n    }\n\n    const loadInitialDoubleData = async () => {\n        return await loadData(path, config)\n    }\n    let data = {} as any\n\n    const apiMap = await getApiMap(path)\n\n    apiMap.getters.forEach(entry => {\n        data[entry] = ref(null)\n    })\n    const setData = (newData) => {\n        Object.entries(newData).forEach(([key, value]) => {\n            if(data[key] === undefined) {\n                data[key] = ref(value)\n            } else {\n                data[key].value = value\n            }\n        })\n    }\n    setData(await loadInitialDoubleData())\n\n    const isLoading = reactive<Record<string, boolean>>({})\n\n    // TODO: Only re-request the entrypoints where their config has actually changed\n    watch(config, async () => {\n        setData(await loadInitialDoubleData())\n    }, {\n        deep: true,\n    })\n\n    const actions = {}\n    apiMap.actions.forEach(method => {\n        isLoading[method] = false\n        actions[method] = async function(data: Record<string, unknown>) {\n            isLoading[method] = true\n            let result = null\n            try {\n                result = await callAction(path, method, data)\n                isLoading[method] = false\n            } catch (e) {\n                isLoading[method] = false\n                throw e\n            }\n            return result\n        }\n    })\n\n    const refresh = async () => {\n        setData(await loadInitialDoubleData())\n    }\n\n    return {\n        ...data,\n        ...actions,\n        isLoading,\n        refresh,\n    }\n}\n\nexport async function getApiMap(path: string): Promise<{ getters: string[], actions: string[] }> {\n    let apiMap = null\n    if(getBundler() === 'webpack') {\n        // Webpack can't dynamically import files from the root folder, so we have to remove the mandatory /src/ prefix\n        // because we need it to be hardcodet in the import call.\n        path = path.replace(/^\\/?src\\//, '')\n        apiMap = (await import(/* webpackPreload: true */ '/src/' + path + '.php')).default\n    } else {\n        apiMap = (await import(/* @vite-ignore */ path + '.php')).default\n    }\n    if(!apiMap) {\n        console.error(`Could not fetch the ${path}.php file. Try restarting your dev server.`)\n    }\n    return apiMap\n}","export type Bundler = 'webpack' | 'vite' | 'rollup'\nlet bundler: Bundler = 'vite'\n\nexport const setBundler = (newBundler: Bundler): void => {\n    bundler = newBundler\n}\n\nexport const getBundler = (): Bundler => {\n    return bundler\n}\n\nexport const getBundlerFilePrefix = (): string => {\n    if(bundler === 'webpack') {\n        return '/src/'\n    }\n    return ''\n}"]}