"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__, {
    default: ()=>useServiceOptions
});
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
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");
function useServiceOptions(props) {
    const { action = 'list', service, fieldNames } = props;
    const params = (null == service ? void 0 : service.params) || {};
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const { getField } = (0, index_js_namespaceObject.useCollection_deprecated)();
    const { getCollectionFields } = (0, index_js_namespaceObject.useCollectionManager_deprecated)();
    const record = (0, external_record_provider_index_js_namespaceObject.useRecord)();
    const normalizeValues = (0, external_react_namespaceObject.useCallback)((obj)=>{
        if (obj && 'object' == typeof obj) return obj[fieldNames.value];
        return obj;
    }, [
        fieldNames.value
    ]);
    const value = (0, external_react_namespaceObject.useMemo)(()=>{
        if (void 0 === props.value || null === props.value) return;
        if (Array.isArray(props.value)) return props.value.map(normalizeValues);
        return [
            normalizeValues(props.value)
        ];
    }, [
        props.value,
        normalizeValues
    ]);
    const collectionField = (0, external_react_namespaceObject.useMemo)(()=>getField(fieldSchema.name), [
        fieldSchema.name
    ]);
    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) ? {
                    [collectionField.foreignKey]: {
                        $is: null
                    }
                } : null,
                null == params ? void 0 : params.filter
            ]),
            isOToAny && null != sourceValue && !(0, external_filter_provider_index_js_namespaceObject.isInFilterFormBlock)(fieldSchema) && (null == collectionField ? void 0 : collectionField.foreignKey) ? {
                [collectionField.foreignKey]: {
                    $eq: sourceValue
                }
            } : null,
            (null == params ? void 0 : params.filter) && value ? {
                [fieldNames.value]: {
                    ['$in']: value
                }
            } : null
        ], '$or');
    }, [
        null == params ? void 0 : params.filter,
        getCollectionFields,
        collectionField,
        sourceValue,
        value,
        fieldNames.value
    ]);
    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
    ]);
}
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
});
