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_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__ from "../../../../filter-provider/index.mjs";
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";
const useFieldServiceFilter = (filter, originalFilter)=>{
    const [fieldServiceFilter, setFieldServiceFilter] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    const { parseFilter, findVariable } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.useParseDataScopeFilter)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const filterFromSchema = filter;
        const _run = async ()=>{
            const result = await parseFilter((0, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.mergeFilter)([
                filterFromSchema || originalFilter
            ]));
            setFieldServiceFilter(result);
        };
        const run = __WEBPACK_EXTERNAL_MODULE_lodash__["default"].debounce(_run, __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.DEBOUNCE_WAIT);
        _run();
        const dispose = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.reaction)(()=>{
            const flat = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.flatten)(filterFromSchema, {
                breakOn (param) {
                    let { key } = param;
                    return key.startsWith('$') && '$and' !== key && '$or' !== key;
                },
                transformValue (value) {
                    if (!(0, __WEBPACK_EXTERNAL_MODULE__variables_index_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);
        return dispose;
    }, [
        filter,
        findVariable,
        originalFilter,
        parseFilter
    ]);
    return [
        fieldServiceFilter,
        setFieldServiceFilter
    ];
};
export { useFieldServiceFilter };
