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_lodash_cloneDeep__ from "lodash/cloneDeep";
import * as __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__ from "../../../collection-manager/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__ from "../../../filter-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__ from "../../../filter-provider/utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../record-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";
import * as __WEBPACK_EXTERNAL_MODULE__variables_utils_isVariable_mjs__ from "../../../variables/utils/isVariable.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "./context.mjs";
const useInsertSchema = (component)=>{
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { insertAfterBegin } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const insert = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((ss)=>{
        const schema = fieldSchema.reduceProperties((buf, s)=>{
            if (s['x-component'] === 'AssociationField.' + component) return s;
            return buf;
        }, null);
        if (!schema) insertAfterBegin((0, __WEBPACK_EXTERNAL_MODULE_lodash_cloneDeep__["default"])(ss));
    }, [
        component
    ]);
    return insert;
};
function useAssociationFieldContext() {
    return (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getCollectionJoinField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const { parseFilter, findVariable } = (0, __WEBPACK_EXTERNAL_MODULE__schema_settings_index_mjs__.useParseDataScopeFilter)();
    const [fieldServiceFilter, setFieldServiceFilter] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_lodash__.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, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.mergeFilter)([
                filterFromSchema || (null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params.filter)
            ]));
            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_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);
        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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const isOToAny = [
            'oho',
            'o2m'
        ].includes(null == collectionField ? void 0 : collectionField.interface);
        return (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.mergeFilter)([
            (0, __WEBPACK_EXTERNAL_MODULE__filter_provider_utils_mjs__.mergeFilter)([
                isOToAny && !(0, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.isInFilterFormBlock)(fieldSchema) && (null == collectionField ? void 0 : collectionField.foreignKey) && !useOriginalFilter ? {
                    [collectionField.foreignKey]: {
                        $is: null
                    }
                } : null,
                fieldServiceFilter
            ]),
            isOToAny && null != sourceValue && !(0, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.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, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
SubFormContext.displayName = 'SubFormContext';
const SubFormProvider = SubFormContext.Provider;
const useSubFormValue = ()=>{
    const { value, collection } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(SubFormContext) || {};
    return {
        formValue: value,
        collection
    };
};
export { SubFormProvider, useServiceOptions as default, useAssociationFieldContext, useFieldNames, useInsertSchema, useSubFormValue };
