import { useCallback, useMemo } from "react";
import { useFieldSchema } from "@tachybase/schema";
import { useCollectionManager_deprecated, useCollection_deprecated } from "../../../collection-manager/index.mjs";
import { isInFilterFormBlock } from "../../../filter-provider/index.mjs";
import { mergeFilter } from "../../../filter-provider/utils.mjs";
import { useRecord } from "../../../record-provider/index.mjs";
function useServiceOptions(props) {
    const { action = 'list', service, fieldNames } = props;
    const params = (null == service ? void 0 : service.params) || {};
    const fieldSchema = useFieldSchema();
    const { getField } = useCollection_deprecated();
    const { getCollectionFields } = useCollectionManager_deprecated();
    const record = useRecord();
    const normalizeValues = useCallback((obj)=>{
        if (obj && 'object' == typeof obj) return obj[fieldNames.value];
        return obj;
    }, [
        fieldNames.value
    ]);
    const value = 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 = useMemo(()=>getField(fieldSchema.name), [
        fieldSchema.name
    ]);
    const sourceValue = null == record ? void 0 : record[null == collectionField ? void 0 : collectionField.sourceKey];
    const filter = useMemo(()=>{
        const isOToAny = [
            'oho',
            'o2m'
        ].includes(null == collectionField ? void 0 : collectionField.interface);
        return mergeFilter([
            mergeFilter([
                isOToAny && !isInFilterFormBlock(fieldSchema) && (null == collectionField ? void 0 : collectionField.foreignKey) ? {
                    [collectionField.foreignKey]: {
                        $is: null
                    }
                } : null,
                null == params ? void 0 : params.filter
            ]),
            isOToAny && null != sourceValue && !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 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 };
