import { Fragment, jsx } from "react/jsx-runtime";
import { Fragment as external_react_Fragment, useMemo } from "react";
import { RecursionField, markRaw, model, observer, useField, useFieldSchema } from "@tachybase/schema";
import { Badge, Tabs } from "antd";
import classnames from "classnames";
import { usePrefixCls } from "../__builtins__/index.mjs";
import { useCompile } from "../../hooks/index.mjs";
const useTabs = ()=>{
    const tabsField = useField();
    const schema = useFieldSchema();
    const tabs = [];
    schema.mapProperties((schema, name)=>{
        var _schema_xcomponent;
        const field = tabsField.query(tabsField.address.concat(name)).take();
        if ((null == field ? void 0 : field.display) === 'none' || (null == field ? void 0 : field.display) === 'hidden') return;
        if ((null == (_schema_xcomponent = schema['x-component']) ? void 0 : _schema_xcomponent.indexOf('TabPane')) > -1) {
            var _field_componentProps, _schema_xcomponentprops;
            const key = (null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.key) || (null == schema ? void 0 : null == (_schema_xcomponentprops = schema['x-component-props']) ? void 0 : _schema_xcomponentprops.key) || name;
            tabs.push({
                name,
                props: {
                    ...null == schema ? void 0 : schema['x-component-props'],
                    ...null == field ? void 0 : field.componentProps,
                    key
                },
                schema
            });
        }
    });
    return tabs;
};
const useTabExtraContent = ()=>{
    const tabsField = useField();
    const schema = useFieldSchema();
    const tabs = [];
    schema.mapProperties((schema, name)=>{
        var _schema_xcomponent;
        const field = tabsField.query(tabsField.address.concat(name)).take();
        if ((null == field ? void 0 : field.display) === 'none' || (null == field ? void 0 : field.display) === 'hidden') return;
        if ((null == (_schema_xcomponent = schema['x-component']) ? void 0 : _schema_xcomponent.indexOf('TabExtraContent')) > -1) {
            var _field_componentProps, _schema_xcomponentprops;
            const key = (null == field ? void 0 : null == (_field_componentProps = field.componentProps) ? void 0 : _field_componentProps.key) || (null == schema ? void 0 : null == (_schema_xcomponentprops = schema['x-component-props']) ? void 0 : _schema_xcomponentprops.key) || name;
            tabs.push({
                name,
                props: {
                    ...null == schema ? void 0 : schema['x-component-props'],
                    ...null == field ? void 0 : field.componentProps,
                    key
                },
                schema
            });
        }
    });
    return tabs;
};
const FeedbackBadge = observer((props)=>{
    const field = useField();
    const compile = useCompile();
    const errors = field.form.queryFeedbacks({
        type: 'error',
        address: "".concat(field.address.concat(props.name), ".*")
    });
    if (errors.length) return /*#__PURE__*/ jsx(Badge, {
        size: "small",
        className: "errors-badge",
        count: errors.length,
        children: compile(props.tab)
    });
    return /*#__PURE__*/ jsx(Fragment, {
        children: compile(props.tab)
    });
});
const createFormTab = (defaultActiveKey)=>{
    const formTab = model({
        activeKey: defaultActiveKey,
        setActiveKey (key) {
            formTab.activeKey = key;
        }
    });
    return markRaw(formTab);
};
const FormTab = observer((param)=>{
    let { formTab, ...props } = param;
    const tabs = useTabs();
    const tabExtraContent = useTabExtraContent();
    const _formTab = useMemo(()=>formTab ? formTab : createFormTab(), []);
    const prefixCls = usePrefixCls('formily-tab', props);
    const activeKey = props.activeKey || (null == _formTab ? void 0 : _formTab.activeKey);
    return /*#__PURE__*/ jsx(Tabs, {
        ...props,
        tabBarExtraContent: /*#__PURE__*/ jsx(Fragment, {
            children: tabExtraContent.map((param, key)=>{
                let { schema, name } = param;
                return /*#__PURE__*/ jsx(RecursionField, {
                    schema: schema,
                    name: name
                }, key);
            })
        }),
        className: classnames(prefixCls, props.className),
        activeKey: activeKey,
        onChange: (key)=>{
            var _props_onChange, _formTab_setActiveKey;
            null == (_props_onChange = props.onChange) || _props_onChange.call(props, key);
            null == _formTab || null == (_formTab_setActiveKey = _formTab.setActiveKey) || _formTab_setActiveKey.call(_formTab, key);
        },
        items: tabs.map((param, key)=>{
            let { props, schema, name } = param;
            return {
                key,
                label: /*#__PURE__*/ jsx(FeedbackBadge, {
                    name: name,
                    tab: props.tab
                }),
                ...props,
                children: /*#__PURE__*/ jsx(RecursionField, {
                    schema: schema,
                    name: name
                })
            };
        })
    });
});
const TabPane = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ jsx(external_react_Fragment, {
        children: children
    });
};
const TabExtraContent = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ jsx(external_react_Fragment, {
        children: children
    });
};
FormTab.TabPane = TabPane;
FormTab.TabExtraContent = TabExtraContent;
FormTab.createFormTab = createFormTab;
const form_tab = FormTab;
export { FormTab, form_tab as default };
