import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__ from "../../schema-settings/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_getPath_mjs__ from "../../variables/utils/getPath.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";
function useParsedFilter(param) {
    let { filterOption } = param;
    const { parseFilter, findVariable } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.useParseDataScopeFilter)();
    const [filter, setFilter] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useDeepCompareEffect)(()=>{
        if (!filterOption) return;
        const _run = async ()=>{
            const result = await parseFilter(filterOption);
            setFilter(result);
        };
        _run();
        const run = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].debounce(_run, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.DEBOUNCE_WAIT);
        (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.reaction)(()=>{
            const flat = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.flatten)(filterOption, {
                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;
                    const variableName = (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_getVariableName_mjs__.getVariableName)(value);
                    const variable = findVariable(variableName);
                    const result = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.getValuesByPath)({
                        [variableName]: (null == variable ? void 0 : variable.ctx) || {}
                    }, (0, __WEBPACK_EXTERNAL_MODULE__variables_utils_getPath_mjs__.getPath)(value));
                    return result;
                }
            });
            return flat;
        }, run);
    }, [
        filterOption
    ]);
    return {
        filter
    };
}
export { useParsedFilter };
