import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__ from "../__builtins__/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
const useTabs = ()=>{
    const tabsField = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_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 ? 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 ? void 0 : _field_componentProps.key) || (null == schema ? void 0 : null === (_schema_xcomponentprops = schema['x-component-props']) || void 0 === _schema_xcomponentprops ? 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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const schema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_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 ? 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 ? void 0 : _field_componentProps.key) || (null == schema ? void 0 : null === (_schema_xcomponentprops = schema['x-component-props']) || void 0 === _schema_xcomponentprops ? 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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const errors = field.form.queryFeedbacks({
        type: 'error',
        address: "".concat(field.address.concat(props.name), ".*")
    });
    if (errors.length) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Badge, {
        size: "small",
        className: "errors-badge",
        count: errors.length,
        children: compile(props.tab)
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: compile(props.tab)
    });
});
const createFormTab = (defaultActiveKey)=>{
    const formTab = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.model)({
        activeKey: defaultActiveKey,
        setActiveKey (key) {
            formTab.activeKey = key;
        }
    });
    return (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.markRaw)(formTab);
};
const FormTab = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((param)=>{
    let { formTab, ...props } = param;
    const tabs = useTabs();
    const tabExtraContent = useTabExtraContent();
    const _formTab = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>formTab ? formTab : createFormTab(), []);
    const prefixCls = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.usePrefixCls)('formily-tab', props);
    const activeKey = props.activeKey || (null == _formTab ? void 0 : _formTab.activeKey);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tabs, {
        ...props,
        tabBarExtraContent: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
            children: tabExtraContent.map((param, key)=>{
                let { schema, name } = param;
                return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                    schema: schema,
                    name: name
                }, key);
            })
        }),
        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])(prefixCls, props.className),
        activeKey: activeKey,
        onChange: (key)=>{
            var _props_onChange, _formTab_setActiveKey;
            null === (_props_onChange = props.onChange) || void 0 === _props_onChange || _props_onChange.call(props, key);
            null == _formTab || null === (_formTab_setActiveKey = _formTab.setActiveKey) || void 0 === _formTab_setActiveKey || _formTab_setActiveKey.call(_formTab, key);
        },
        items: tabs.map((param, key)=>{
            let { props, schema, name } = param;
            return {
                key,
                label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(FeedbackBadge, {
                    name: name,
                    tab: props.tab
                }),
                ...props,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                    schema: schema,
                    name: name
                })
            };
        })
    });
});
const TabPane = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
        children: children
    });
};
const TabExtraContent = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react__.Fragment, {
        children: children
    });
};
FormTab.TabPane = TabPane;
FormTab.TabExtraContent = TabExtraContent;
FormTab.createFormTab = createFormTab;
const form_tab_rslib_entry_ = FormTab;
export { FormTab, form_tab_rslib_entry_ as default };
