"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    SubFormProvider: ()=>SubFormProvider,
    useAssociationFieldContext: ()=>useAssociationFieldContext,
    useSubFormValue: ()=>useSubFormValue,
    useFieldNames: ()=>useFieldNames,
    useInsertSchema: ()=>useInsertSchema,
    default: ()=>useServiceOptions
});
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const client_namespaceObject = require("@tachybase/utils/client");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const cloneDeep_namespaceObject = require("lodash/cloneDeep");
var cloneDeep_default = /*#__PURE__*/ __webpack_require__.n(cloneDeep_namespaceObject);
const index_js_namespaceObject = require("../../../collection-manager/index.js");
const external_filter_provider_index_js_namespaceObject = require("../../../filter-provider/index.js");
const utils_js_namespaceObject = require("../../../filter-provider/utils.js");
const external_record_provider_index_js_namespaceObject = require("../../../record-provider/index.js");
const external_schema_settings_index_js_namespaceObject = require("../../../schema-settings/index.js");
const external_variables_index_js_namespaceObject = require("../../../variables/index.js");
const getPath_js_namespaceObject = require("../../../variables/utils/getPath.js");
const getVariableName_js_namespaceObject = require("../../../variables/utils/getVariableName.js");
const isVariable_js_namespaceObject = require("../../../variables/utils/isVariable.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_context_js_namespaceObject = require("./context.js");
const useInsertSchema = (component)=>{
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { insertAfterBegin } = (0, external_hooks_index_js_namespaceObject.useDesignable)();
    const insert = (0, external_react_namespaceObject.useCallback)((ss)=>{
        const schema = fieldSchema.reduceProperties((buf, s)=>{
            if (s['x-component'] === 'AssociationField.' + component) return s;
            return buf;
        }, null);
        if (!schema) insertAfterBegin(cloneDeep_default()(ss));
    }, [
        component
    ]);
    return insert;
};
function useAssociationFieldContext() {
    return (0, external_react_namespaceObject.useContext)(external_context_js_namespaceObject.AssociationFieldContext);
}
function useServiceOptions(props) {
    var _field_componentProps_service_params, _field_componentProps_service, _field_componentProps, _service_params;
    const { action = 'list', service, useOriginalFilter } = props;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const field = (0, schema_namespaceObject.useField)();
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const { parseFilter, findVariable } = (0, external_schema_settings_index_js_namespaceObject.useParseDataScopeFilter)();
    const [fieldServiceFilter, setFieldServiceFilter] = (0, external_react_namespaceObject.useState)(null);
    (0, external_react_namespaceObject.useEffect)(()=>{
        var _fieldSchema_xcomponentprops_service_params, _fieldSchema_xcomponentprops_service, _fieldSchema_xcomponentprops, _field_componentProps_service_params, _field_componentProps_service, _field_componentProps, _fieldSchema_xcomponentprops_service_params1, _fieldSchema_xcomponentprops_service1, _fieldSchema_xcomponentprops1;
        const filterFromSchema = (0, external_lodash_namespaceObject.isString)(null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_service = _fieldSchema_xcomponentprops.service) || void 0 === _fieldSchema_xcomponentprops_service ? void 0 : null === (_fieldSchema_xcomponentprops_service_params = _fieldSchema_xcomponentprops_service.params) || void 0 === _fieldSchema_xcomponentprops_service_params ? void 0 : _fieldSchema_xcomponentprops_service_params.filter) ? null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : null === (_field_componentProps_service = _field_componentProps.service) || void 0 === _field_componentProps_service ? void 0 : null === (_field_componentProps_service_params = _field_componentProps_service.params) || void 0 === _field_componentProps_service_params ? void 0 : _field_componentProps_service_params.filter : null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : null === (_fieldSchema_xcomponentprops_service1 = _fieldSchema_xcomponentprops1.service) || void 0 === _fieldSchema_xcomponentprops_service1 ? void 0 : null === (_fieldSchema_xcomponentprops_service_params1 = _fieldSchema_xcomponentprops_service1.params) || void 0 === _fieldSchema_xcomponentprops_service_params1 ? void 0 : _fieldSchema_xcomponentprops_service_params1.filter;
        const _run = async ()=>{
            var _service_params;
            const result = await parseFilter((0, utils_js_namespaceObject.mergeFilter)([
                filterFromSchema || (null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params.filter)
            ]));
            setFieldServiceFilter(result);
        };
        const run = external_lodash_default().debounce(_run, external_variables_index_js_namespaceObject.DEBOUNCE_WAIT);
        _run();
        const dispose = (0, schema_namespaceObject.reaction)(()=>{
            const flat = (0, client_namespaceObject.flatten)(filterFromSchema, {
                breakOn (param) {
                    let { key } = param;
                    return key.startsWith('$') && '$and' !== key && '$or' !== key;
                },
                transformValue (value) {
                    if (!(0, isVariable_js_namespaceObject.isVariable)(value)) return value;
                    const variableName = (0, getVariableName_js_namespaceObject.getVariableName)(value);
                    const variable = findVariable(variableName);
                    const result = (0, client_namespaceObject.getValuesByPath)({
                        [variableName]: (null == variable ? void 0 : variable.ctx) || {}
                    }, (0, getPath_js_namespaceObject.getPath)(value));
                    return result;
                }
            });
            return flat;
        }, run);
        return dispose;
    }, [
        null === (_field_componentProps = field.componentProps) || void 0 === _field_componentProps ? void 0 : null === (_field_componentProps_service = _field_componentProps.service) || void 0 === _field_componentProps_service ? void 0 : null === (_field_componentProps_service_params = _field_componentProps_service.params) || void 0 === _field_componentProps_service_params ? void 0 : _field_componentProps_service_params.filter,
        fieldSchema,
        findVariable,
        parseFilter,
        record,
        null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params.filter
    ]);
    const collectionField = (0, external_react_namespaceObject.useMemo)(()=>getField(fieldSchema.name) || getCollectionJoinField(null == fieldSchema ? void 0 : fieldSchema['x-collection-field']), [
        fieldSchema
    ]);
    const sourceValue = null == record ? void 0 : record[null == collectionField ? void 0 : collectionField.sourceKey];
    const filter = (0, external_react_namespaceObject.useMemo)(()=>{
        const isOToAny = [
            'oho',
            'o2m'
        ].includes(null == collectionField ? void 0 : collectionField.interface);
        return (0, utils_js_namespaceObject.mergeFilter)([
            (0, utils_js_namespaceObject.mergeFilter)([
                isOToAny && !(0, external_filter_provider_index_js_namespaceObject.isInFilterFormBlock)(fieldSchema) && (null == collectionField ? void 0 : collectionField.foreignKey) && !useOriginalFilter ? {
                    [collectionField.foreignKey]: {
                        $is: null
                    }
                } : null,
                fieldServiceFilter
            ]),
            isOToAny && null != sourceValue && !(0, external_filter_provider_index_js_namespaceObject.isInFilterFormBlock)(fieldSchema) && (null == collectionField ? void 0 : collectionField.foreignKey) && !useOriginalFilter ? {
                [collectionField.foreignKey]: {
                    $eq: sourceValue
                }
            } : null
        ], '$or');
    }, [
        null == collectionField ? void 0 : collectionField.interface,
        null == collectionField ? void 0 : collectionField.foreignKey,
        fieldSchema,
        fieldServiceFilter,
        sourceValue,
        useOriginalFilter
    ]);
    return (0, external_react_namespaceObject.useMemo)(()=>({
            resource: null == collectionField ? void 0 : collectionField.target,
            action,
            ...service,
            params: {
                ...null == service ? void 0 : service.params,
                filter
            }
        }), [
        null == collectionField ? void 0 : collectionField.target,
        action,
        filter,
        service
    ]);
}
const useFieldNames = (props)=>{
    var _fieldSchema_xcomponentprops_field_uiSchema_xcomponentprops, _fieldSchema_xcomponentprops_field_uiSchema, _fieldSchema_xcomponentprops_field, _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1;
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const fieldNames = (null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : null === (_fieldSchema_xcomponentprops_field = _fieldSchema_xcomponentprops['field']) || void 0 === _fieldSchema_xcomponentprops_field ? void 0 : null === (_fieldSchema_xcomponentprops_field_uiSchema = _fieldSchema_xcomponentprops_field['uiSchema']) || void 0 === _fieldSchema_xcomponentprops_field_uiSchema ? void 0 : null === (_fieldSchema_xcomponentprops_field_uiSchema_xcomponentprops = _fieldSchema_xcomponentprops_field_uiSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops_field_uiSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops_field_uiSchema_xcomponentprops['fieldNames']) || (null == fieldSchema ? void 0 : null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1['fieldNames']) || props.fieldNames;
    return {
        label: 'label',
        value: 'value',
        ...fieldNames
    };
};
const SubFormContext = (0, external_react_namespaceObject.createContext)(null);
SubFormContext.displayName = 'SubFormContext';
const SubFormProvider = SubFormContext.Provider;
const useSubFormValue = ()=>{
    const { value, collection } = (0, external_react_namespaceObject.useContext)(SubFormContext) || {};
    return {
        formValue: value,
        collection
    };
};
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
});
