"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(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 = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != 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__, {
    useParseDataScopeFilter: ()=>useParseDataScopeFilter
});
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/utils/client");
const index_js_namespaceObject = require("../../variables/index.js");
const getVariableName_js_namespaceObject = require("../../variables/utils/getVariableName.js");
const isVariable_js_namespaceObject = require("../../variables/utils/isVariable.js");
const defaultExclude = [
    '$user',
    '$date',
    '$nDate',
    '$nRole'
];
const useParseDataScopeFilter = function() {
    let { exclude = defaultExclude } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    const localVariables = (0, index_js_namespaceObject.useLocalVariables)();
    const variables = (0, index_js_namespaceObject.useVariables)();
    const findVariable = (0, external_react_namespaceObject.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, external_react_namespaceObject.useCallback)(async (filterValue)=>{
        const flat = (0, client_namespaceObject.flatten)(filterValue, {
            breakOn (param) {
                let { key } = param;
                return key.startsWith('$') && '$and' !== key && '$or' !== key;
            },
            transformValue (value) {
                if (!(0, isVariable_js_namespaceObject.isVariable)(value)) return value;
                if (exclude.includes((0, getVariableName_js_namespaceObject.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, client_namespaceObject.unflatten)(flat);
        return result;
    }, [
        exclude,
        localVariables,
        null == variables ? void 0 : variables.parseVariable
    ]);
    return {
        parseFilter,
        findVariable
    };
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
