import { jsx } from "react/jsx-runtime";
import react, { useContext, useMemo } from "react";
import { useFieldSchema } from "@tachybase/schema";
import { useFormBlockContext, useFormBlockType } from "../../block-provider/index.mjs";
import { useCollection_deprecated } from "../../collection-manager/hooks/useCollection_deprecated.mjs";
import { useCollectionManager_deprecated } from "../../collection-manager/hooks/useCollectionManager_deprecated.mjs";
import { isPatternDisabled } from "../isPatternDisabled.mjs";
import { isSystemField } from "../SchemaSettings.mjs";
const DefaultValueContext = /*#__PURE__*/ react.createContext(null);
const DefaultValueProvider = (props)=>{
    const value = useMemo(()=>({
            isAllowToSetDefaultValue: props.isAllowToSetDefaultValue
        }), [
        props.isAllowToSetDefaultValue
    ]);
    return /*#__PURE__*/ jsx(DefaultValueContext.Provider, {
        value: value,
        children: props.children
    });
};
const useIsAllowToSetDefaultValue = function() {
    let { form, fieldSchema, collectionField } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
    const { getInterface, getCollectionJoinField } = useCollectionManager_deprecated();
    const { getField } = useCollection_deprecated();
    const { form: innerForm } = useFormBlockContext();
    const innerFieldSchema = useFieldSchema();
    const { type } = useFormBlockType();
    const { isAllowToSetDefaultValue = _isAllowToSetDefaultValue } = useContext(DefaultValueContext) || {};
    const innerCollectionField = getField(innerFieldSchema['name']) || getCollectionJoinField(innerFieldSchema['x-collection-field']);
    return {
        isAllowToSetDefaultValue: (isSubTableColumn)=>isAllowToSetDefaultValue({
                collectionField: collectionField || innerCollectionField,
                getInterface,
                form: form || innerForm,
                formBlockType: type,
                fieldSchema: fieldSchema || innerFieldSchema,
                isSubTableColumn
            })
    };
};
const interfacesOfUnsupportedDefaultValue = [
    'o2o',
    'oho',
    'obo',
    'o2m',
    'attachment',
    'expression',
    'point',
    'lineString',
    'circle',
    'polygon',
    'sequence',
    'formula'
];
function _isAllowToSetDefaultValue(param) {
    let { collectionField, getInterface, formBlockType, form, fieldSchema, isSubTableColumn } = param;
    var _fieldSchema_xcomponentprops;
    if (isSubTableColumn) return !interfacesOfUnsupportedDefaultValue.includes(null == collectionField ? void 0 : collectionField.interface) && !isSystemField(collectionField, getInterface);
    if (!collectionField) return false;
    if (collectionField.target && (null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.mode) && ![
        'Picker',
        'Select'
    ].includes(fieldSchema['x-component-props'].mode)) return false;
    if ('create' !== formBlockType) return false;
    return !(null == form ? void 0 : form.readPretty) && !isPatternDisabled(fieldSchema) && !interfacesOfUnsupportedDefaultValue.includes(null == collectionField ? void 0 : collectionField.interface) && !isSystemField(collectionField, getInterface);
}
export { DefaultValueProvider, interfacesOfUnsupportedDefaultValue, useIsAllowToSetDefaultValue };
