'use client';
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import React, { useCallback, useMemo } from 'react';
import { useField } from '../../forms/useField/index.js';
import { withCondition } from '../../forms/withCondition/index.js';
import { mergeFieldStyles } from '../mergeFieldStyles.js';
import { RelationshipInput } from './Input.js';
import './index.scss';
export { RelationshipInput };
const RelationshipFieldComponent = (props)=>{
    const { field, field: { admin: { allowCreate = true, allowEdit = true, appearance = 'select', className, description, isSortable = true, placeholder, sortOptions } = {}, hasMany, label, localized, relationTo: relationToProp, required }, path: pathFromProps, readOnly, validate } = props;
    const memoizedValidate = useCallback((value, validationOptions)=>{
        if (typeof validate === 'function') {
            return validate(value, {
                ...validationOptions,
                required
            });
        }
    }, [
        validate,
        required
    ]);
    const { customComponents: { AfterInput, BeforeInput, Description, Error, Label } = {}, disabled, filterOptions, initialValue, path, setValue, showError, value } = useField({
        potentiallyStalePath: pathFromProps,
        validate: memoizedValidate
    });
    const styles = useMemo(()=>mergeFieldStyles(field), [
        field
    ]);
    const isPolymorphic = Array.isArray(relationToProp);
    const [relationTo] = React.useState(()=>Array.isArray(relationToProp) ? relationToProp : [
            relationToProp
        ]);
    const handleChangeHasMulti = useCallback((newValue)=>{
        if (!newValue) {
            setValue(null, newValue === value);
            return;
        }
        let disableFormModification = false;
        if (isPolymorphic) {
            disableFormModification = Array.isArray(value) && Array.isArray(newValue) && value.length === newValue.length && value.every((val, idx)=>{
                const newVal = newValue[idx];
                return val.value === newVal.value && val.relationTo === newVal.relationTo;
            });
        } else {
            disableFormModification = Array.isArray(value) && Array.isArray(newValue) && value.length === newValue.length && value.every((val, idx)=>val === newValue[idx].value);
        }
        const dataToSet = newValue.map((val)=>{
            if (isPolymorphic) {
                return val;
            } else {
                return val.value;
            }
        });
        setValue(dataToSet, disableFormModification);
    }, [
        isPolymorphic,
        setValue,
        value
    ]);
    const handleChangeSingle = useCallback((newValue)=>{
        if (!newValue) {
            setValue(null, newValue === value);
            return;
        }
        let disableFormModification = false;
        if (isPolymorphic) {
            disableFormModification = value && newValue && value.value === newValue.value && value.relationTo === newValue.relationTo;
        } else {
            disableFormModification = value && newValue && value === newValue.value;
        }
        const dataToSet = isPolymorphic ? newValue : newValue.value;
        setValue(dataToSet, disableFormModification);
    }, [
        isPolymorphic,
        setValue,
        value
    ]);
    const memoizedValue = React.useMemo(()=>{
        if (hasMany === true) {
            return Array.isArray(value) ? value.map((val)=>{
                return isPolymorphic ? val : {
                    relationTo: Array.isArray(relationTo) ? relationTo[0] : relationTo,
                    value: val
                };
            }) : value;
        } else {
            return value ? isPolymorphic ? value : {
                relationTo: Array.isArray(relationTo) ? relationTo[0] : relationTo,
                value
            } : value;
        }
    }, [
        hasMany,
        value,
        isPolymorphic,
        relationTo
    ]);
    const memoizedInitialValue = React.useMemo(()=>{
        if (hasMany === true) {
            return Array.isArray(initialValue) ? initialValue.map((val)=>{
                return isPolymorphic ? val : {
                    relationTo: Array.isArray(relationTo) ? relationTo[0] : relationTo,
                    value: val
                };
            }) : initialValue;
        } else {
            return initialValue ? isPolymorphic ? initialValue : {
                relationTo: Array.isArray(relationTo) ? relationTo[0] : relationTo,
                value: initialValue
            } : initialValue;
        }
    }, [
        initialValue,
        isPolymorphic,
        relationTo,
        hasMany
    ]);
    return /*#__PURE__*/ _jsxDEV(RelationshipInput, {
        AfterInput: AfterInput,
        allowCreate: allowCreate,
        allowEdit: allowEdit,
        appearance: appearance,
        BeforeInput: BeforeInput,
        className: className,
        Description: Description,
        description: description,
        Error: Error,
        filterOptions: filterOptions,
        formatDisplayedOptions: isPolymorphic ? undefined : (options)=>options.map((opt)=>opt.options).flat(),
        isSortable: isSortable,
        Label: Label,
        label: label,
        localized: localized,
        maxResultsPerRequest: 10,
        maxRows: field?.maxRows,
        minRows: field?.minRows,
        path: path,
        placeholder: placeholder,
        readOnly: readOnly || disabled,
        relationTo: relationTo,
        required: required,
        showError: showError,
        sortOptions: sortOptions,
        style: styles,
        ...hasMany === true ? {
            hasMany: true,
            initialValue: memoizedInitialValue,
            onChange: handleChangeHasMulti,
            value: memoizedValue
        } : {
            hasMany: false,
            initialValue: memoizedInitialValue,
            onChange: handleChangeSingle,
            value: memoizedValue
        }
    }, void 0, false, {
        fileName: "src/fields/Relationship/index.tsx",
        lineNumber: 189,
        columnNumber: 5
    }, this);
};
export const RelationshipField = withCondition(RelationshipFieldComponent);

//# sourceMappingURL=index.js.map