import { jsx } from "react/jsx-runtime";
import { useEffect, useMemo } from "react";
import { useField, useFieldSchema } from "@tachybase/schema";
import { FormItem } from "@tego/client";
import { createStyles } from "antd-style";
import classnames from "classnames";
import { useApp } from "../../../application/index.mjs";
import { useFormActiveFields } from "../../../block-provider/hooks/useFormActiveFields.mjs";
import { ACLCollectionFieldProvider } from "../../../built-in/acl/index.mjs";
import { CollectionFieldProvider, useContextConfigSetting } from "../../../data-source/index.mjs";
import { GeneralSchemaDesigner } from "../../../schema-settings/index.mjs";
import { useVariables } from "../../../variables/index.mjs";
import useContextVariable from "../../../variables/hooks/useContextVariable.mjs";
import { BlockItem } from "../block-item/index.mjs";
import { HTMLEncode } from "../input/shared.mjs";
import { FilterFormDesigner } from "./FormItem.FilterFormDesigner.mjs";
import useLazyLoadDisplayAssociationFieldsOfForm from "./hooks/useLazyLoadDisplayAssociationFieldsOfForm.mjs";
import useParseDefaultValue from "./hooks/useParseDefaultValue.mjs";
import { useEnsureOperatorsValid } from "./SchemaSettingOptions.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n      & .ant-space {\n        flex-wrap: wrap;\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
function _templateObject1() {
    const data = _tagged_template_literal([
        "\n      > .ant-formily-item-label {\n        display: none;\n      }\n    "
    ]);
    _templateObject1 = function() {
        return data;
    };
    return data;
}
function _templateObject2() {
    const data = _tagged_template_literal([
        "\n      display: flex;\n      flex-direction: row;\n      align-items: baseline;\n    "
    ]);
    _templateObject2 = function() {
        return data;
    };
    return data;
}
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        space: css(_templateObject()),
        hide: css(_templateObject1()),
        row: css(_templateObject2())
    };
});
const FormItem_FormItem = (props)=>{
    var _schema_xdecoratorprops;
    const { styles } = useStyles();
    const { layoutDirection: selfLayoutDirection } = props;
    const field = useField();
    const schema = useFieldSchema();
    const contextVariable = useContextVariable();
    const variables = useVariables();
    const { addActiveFieldName } = useFormActiveFields() || {};
    const { layoutDirection } = useContextConfigSetting();
    const finishLayoutDirection = null != selfLayoutDirection ? selfLayoutDirection : layoutDirection;
    useEnsureOperatorsValid();
    useEffect(()=>{
        null == variables || variables.registerVariable(contextVariable);
    }, [
        contextVariable
    ]);
    useParseDefaultValue();
    useLazyLoadDisplayAssociationFieldsOfForm();
    useEffect(()=>{
        null == addActiveFieldName || addActiveFieldName(schema.name);
    }, [
        addActiveFieldName,
        schema.name
    ]);
    var _schema_xdecoratorprops_showTitle;
    const showTitle = null != (_schema_xdecoratorprops_showTitle = null == (_schema_xdecoratorprops = schema['x-decorator-props']) ? void 0 : _schema_xdecoratorprops.showTitle) ? _schema_xdecoratorprops_showTitle : true;
    const extra = useMemo(()=>{
        if ('string' == typeof field.description) {
            const desc = field.description.trim();
            if (!desc) return;
            return /*#__PURE__*/ jsx("div", {
                dangerouslySetInnerHTML: {
                    __html: HTMLEncode(desc).split('\n').join('<br/>')
                }
            });
        }
        var _field_description;
        return null != (_field_description = field.description) ? _field_description : void 0;
    }, [
        field.description
    ]);
    const className = useMemo(()=>classnames(styles.space, {
            [styles.hide]: false === showTitle
        }, {
            [styles.row]: 'row' === finishLayoutDirection
        }), [
        showTitle
    ]);
    return /*#__PURE__*/ jsx(CollectionFieldProvider, {
        allowNull: true,
        children: /*#__PURE__*/ jsx(BlockItem, {
            className: 'tb-form-item',
            children: /*#__PURE__*/ jsx(ACLCollectionFieldProvider, {
                children: /*#__PURE__*/ jsx(FormItem, {
                    className: className,
                    ...props,
                    extra: extra
                })
            })
        })
    });
};
FormItem_FormItem.displayName = 'FormItem';
FormItem_FormItem.Designer = function() {
    const app = useApp();
    const fieldSchema = useFieldSchema();
    const settingsName = "FormItemSettings:".concat(fieldSchema['x-interface']);
    const defaultActionSettings = 'FormItemSettings';
    const hasFieldItem = app.schemaSettingsManager.has(settingsName);
    return /*#__PURE__*/ jsx(GeneralSchemaDesigner, {
        schemaSettings: hasFieldItem ? settingsName : defaultActionSettings
    });
};
function isFileCollection(collection) {
    return (null == collection ? void 0 : collection.template) === 'file';
}
FormItem_FormItem.FilterFormDesigner = FilterFormDesigner;
export { FormItem_FormItem as FormItem, isFileCollection };
