import { jsx as _jsx } from "@emotion/react/jsx-runtime";
import React from "react";
import { useLocation } from "react-router-dom";
import { evaluate } from "./hooks/useExpression";
import { useQRScannerAction } from "./QRScanner.action";
import * as queryString from "./utils/query-string";
import { useVariablesContext, VariablesProvider, RuntimeContext, } from "@qorebase/app-component";
import { usePageParam } from "./hooks/usePageParam";
export const ParamsProvider = (props) => {
    const location = useLocation();
    const params = React.useMemo(() => queryString.parse(location.search), [location.search]);
    const modalPage = React.useMemo(() => {
        return params.modalPage;
    }, [params]);
    const pageParam = usePageParam();
    const page = RuntimeContext.useSelectState((state) => state.maps.page[modalPage || pageParam]);
    const { code } = useQRScannerAction();
    const { values } = useVariablesContext();
    const val = React.useMemo(() => {
        return Object.assign(Object.assign({}, code), values);
    }, [code, values]);
    const pageParams = React.useMemo(() => {
        var _a;
        return Object.fromEntries(Object.entries((_a = page === null || page === void 0 ? void 0 : page.params) !== null && _a !== void 0 ? _a : {}).map(([key, defaultValue]) => {
            var _a;
            return [
                `params.${key}`,
                evaluate((_a = params[key]) !== null && _a !== void 0 ? _a : defaultValue, val),
            ];
        }));
    }, [page === null || page === void 0 ? void 0 : page.params, params, val]);
    return (_jsx(VariablesProvider, Object.assign({ values: pageParams }, { children: props.children })));
};
//# sourceMappingURL=ParamsProvider.js.map