import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
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";
function useServiceOptions(props) {
    const { action = 'list', service, fieldNames } = props;
    const params = (null == service ? void 0 : service.params) || {};
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const { getField } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollection_deprecated)();
    const { getCollectionFields } = (0, __WEBPACK_EXTERNAL_MODULE__collection_manager_index_mjs__.useCollectionManager_deprecated)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    const normalizeValues = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((obj)=>{
        if (obj && 'object' == typeof obj) return obj[fieldNames.value];
        return obj;
    }, [
        fieldNames.value
    ]);
    const value = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getField(fieldSchema.name), [
        fieldSchema.name
    ]);
    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) ? {
                    [collectionField.foreignKey]: {
                        $is: null
                    }
                } : null,
                null == params ? void 0 : params.filter
            ]),
            isOToAny && null != sourceValue && !(0, __WEBPACK_EXTERNAL_MODULE__filter_provider_index_mjs__.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, __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
    ]);
}
export { useServiceOptions as default };
