import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_getVariableName_mjs__ from "../../variables/utils/getVariableName.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__ from "../../variables/utils/isVariable.mjs";
const defaultExclude = [
    '$user',
    '$date',
    '$nDate',
    '$nRole'
];
const useParseDataScopeFilter = function() {
    let { exclude = defaultExclude } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    const localVariables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useLocalVariables)();
    const variables = (0, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.useVariables)();
    const findVariable = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((name)=>{
        let result = null == variables ? void 0 : variables.getVariable(name);
        if (!result) result = localVariables.find((item)=>item.name === name);
        return result;
    }, [
        localVariables,
        variables
    ]);
    const parseFilter = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async (filterValue)=>{
        const flat = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.flatten)(filterValue, {
            breakOn (param) {
                let { key } = param;
                return key.startsWith('$') && '$and' !== key && '$or' !== key;
            },
            transformValue (value) {
                if (!(0, __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__.isVariable)(value)) return value;
                if (exclude.includes((0, __WEBPACK_EXTERNAL_MODULE__variables_utils_getVariableName_mjs__.getVariableName)(value))) return value;
                const result = null == variables ? void 0 : variables.parseVariable(value, localVariables);
                return result;
            }
        });
        await Promise.all(Object.keys(flat).map(async (key)=>{
            flat[key] = await flat[key];
            if (void 0 === flat[key]) flat[key] = null;
            return flat[key];
        }));
        const result = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.unflatten)(flat);
        return result;
    }, [
        exclude,
        localVariables,
        null == variables ? void 0 : variables.parseVariable
    ]);
    return {
        parseFilter,
        findVariable
    };
};
export { useParseDataScopeFilter };
