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_components__ from "@tachybase/components";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE__ant_design_pro_layout__ from "@ant-design/pro-layout";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE_react_error_boundary__ from "react-error-boundary";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__ from "../__builtins__/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_acl_style_mjs__ from "../../../built-in/acl/style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_context_menu_useContextMenu_mjs__ from "../../../built-in/context-menu/useContextMenu.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_document_title_index_mjs__ from "../../../built-in/document-title/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filter_provider_FilterProvider_mjs__ from "../../../filter-provider/FilterProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__icon_index_mjs__ from "../../../icon/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_initializer_hooks_useGetAriaLabelOfSchemaInitializer_mjs__ from "../../../schema-initializer/hooks/useGetAriaLabelOfSchemaInitializer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__ from "../../../style/theme/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_index_mjs__ from "../../common/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_sortable_item_index_mjs__ from "../../common/sortable-item/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__core_index_mjs__ from "../../core/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__error_fallback_index_mjs__ from "../error-fallback/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__FixedBlock_mjs__ from "./FixedBlock.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__PageTabDesigner_mjs__ from "./PageTabDesigner.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
const Page = (props)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _fieldSchema_xcomponentprops2;
    const { children, ...others } = props;
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useCompile)();
    const { title, setTitle } = (0, __WEBPACK_EXTERNAL_MODULE__built_in_document_title_index_mjs__.useDocumentTitle)();
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const dn = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesignable)();
    const { theme } = (0, __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__.useGlobalTheme)();
    const { getAriaLabel } = (0, __WEBPACK_EXTERNAL_MODULE__schema_initializer_hooks_useGetAriaLabelOfSchemaInitializer_mjs__.useGetAriaLabelOfSchemaInitializer)();
    const { hiddenScrollArea } = (0, __WEBPACK_EXTERNAL_MODULE__built_in_context_menu_useContextMenu_mjs__.useContextMenu)();
    const [hasMounted, setHasMounted] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        setTimeout(()=>{
            setHasMounted(true);
        });
    }, []);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!title) setTitle(t(fieldSchema.title));
    }, [
        fieldSchema.title,
        title
    ]);
    const disablePageHeader = null === (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops ? void 0 : _fieldSchema_xcomponentprops.disablePageHeader;
    const enablePageTabs = null === (_fieldSchema_xcomponentprops1 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops1 ? void 0 : _fieldSchema_xcomponentprops1.enablePageTabs;
    const hidePageTitle = null === (_fieldSchema_xcomponentprops2 = fieldSchema['x-component-props']) || void 0 === _fieldSchema_xcomponentprops2 ? void 0 : _fieldSchema_xcomponentprops2.hidePageTitle;
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.SchemaOptionsContext);
    const [searchParams, setSearchParams] = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useSearchParams)();
    const [loading, setLoading] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const activeKey = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>searchParams.get('tab') || Object.keys(fieldSchema.properties || {}).shift(), [
        fieldSchema.properties,
        searchParams
    ]);
    const [height, setHeight] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(0);
    const { wrapSSR, hashId, componentCls } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const aclStyles = (0, __WEBPACK_EXTERNAL_MODULE__built_in_acl_style_mjs__.useStyles)();
    const handleErrors = (error)=>{
        var _window_Sentry, _window;
        null === (_window = window) || void 0 === _window || null === (_window_Sentry = _window.Sentry) || void 0 === _window_Sentry || _window_Sentry.captureException(error);
        console.error(error);
    };
    const pageHeaderTitle = hidePageTitle ? void 0 : fieldSchema.title || compile(title);
    return wrapSSR(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__filter_provider_FilterProvider_mjs__.FilterBlockProvider, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
            className: "".concat(componentCls, " ").concat(hashId, " ").concat(aclStyles.styles),
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__PageTabDesigner_mjs__.PageDesigner, {
                    title: fieldSchema.title || title
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    ref: (ref)=>{
                        setHeight(Math.floor((null == ref ? void 0 : ref.getBoundingClientRect().height) || 0) + 1);
                    },
                    className: "tb-page-header-wrapper",
                    children: !disablePageHeader && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_pro_layout__.PageHeader, {
                        className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])('pageHeaderCss', pageHeaderTitle || enablePageTabs ? '' : 'height0'),
                        ghost: false,
                        title: pageHeaderTitle || ' ',
                        ...others,
                        footer: enablePageTabs && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__common_index_mjs__.DndContext, {
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Tabs, {
                                size: 'small',
                                animated: hasMounted,
                                activeKey: activeKey,
                                onTabClick: (activeKey)=>{
                                    setLoading(true);
                                    setSearchParams([
                                        [
                                            'tab',
                                            activeKey
                                        ]
                                    ]);
                                    setTimeout(()=>{
                                        setLoading(false);
                                    }, 50);
                                },
                                tabBarExtraContent: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                    className: "tb-tabs-wrapper",
                                    children: [
                                        !hiddenScrollArea && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ScrollArea, {}),
                                        dn.designable && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                                            "aria-label": getAriaLabel('tabs'),
                                            icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.PlusOutlined, {}),
                                            className: 'addTabBtn',
                                            type: 'dashed',
                                            onClick: async ()=>{
                                                const values = await (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.FormDialog)(t('Add tab'), ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.SchemaComponentOptions, {
                                                        scope: options.scope,
                                                        components: {
                                                            ...options.components
                                                        },
                                                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_components__.FormLayout, {
                                                            layout: 'vertical',
                                                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.SchemaComponent, {
                                                                schema: {
                                                                    properties: {
                                                                        title: {
                                                                            title: t('Tab name'),
                                                                            'x-component': 'Input',
                                                                            'x-decorator': 'FormItem',
                                                                            required: true
                                                                        },
                                                                        icon: {
                                                                            title: t('Icon'),
                                                                            'x-component': 'IconPicker',
                                                                            'x-decorator': 'FormItem'
                                                                        }
                                                                    }
                                                                }
                                                            })
                                                        })
                                                    }), theme).open({
                                                    initialValues: {}
                                                });
                                                const { title, icon } = values;
                                                dn.insertBeforeEnd({
                                                    type: 'void',
                                                    title,
                                                    'x-icon': icon,
                                                    'x-component': 'Grid',
                                                    'x-initializer': 'page:addBlock',
                                                    properties: {}
                                                });
                                            },
                                            children: t('Add tab')
                                        })
                                    ]
                                }),
                                items: fieldSchema.mapProperties((schema)=>({
                                        label: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__common_sortable_item_index_mjs__.SortableItem, {
                                            id: schema.name,
                                            schema: schema,
                                            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])('tb-action-link', 'designerCss', props.className),
                                            children: [
                                                schema['x-icon'] && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__icon_index_mjs__.Icon, {
                                                    style: {
                                                        marginRight: 8
                                                    },
                                                    type: schema['x-icon']
                                                }),
                                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                                                    children: schema.title || t('Unnamed')
                                                }),
                                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__PageTabDesigner_mjs__.PageTabDesigner, {
                                                    schema: schema
                                                })
                                            ]
                                        }),
                                        key: schema.name
                                    }))
                            })
                        }),
                        extra: !enablePageTabs && !hiddenScrollArea && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ScrollArea, {})
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    className: "tb-page-wrapper",
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_error_boundary__.ErrorBoundary, {
                        FallbackComponent: __WEBPACK_EXTERNAL_MODULE__error_fallback_index_mjs__.ErrorFallback,
                        onError: handleErrors,
                        children: PageContent(loading, disablePageHeader, enablePageTabs, fieldSchema, activeKey, height, props)
                    })
                })
            ]
        })
    }));
};
Page.displayName = 'Page';
function PageContent(loading, disablePageHeader, enablePageTabs, fieldSchema, activeKey, height, props) {
    const { token } = (0, __WEBPACK_EXTERNAL_MODULE__builtins_index_mjs__.useToken)();
    if (loading) return;
    return !disablePageHeader && enablePageTabs ? fieldSchema.mapProperties((schema)=>{
        if (schema.name !== activeKey) return null;
        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__FixedBlock_mjs__["default"], {
            height: "calc(".concat(height, "px + 46px + ").concat(token.marginLG, "px * 2)"),
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.SchemaComponent, {
                schema: new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
                    properties: {
                        [schema.name]: schema
                    }
                })
            })
        }, schema.name);
    }) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__FixedBlock_mjs__["default"], {
        height: "calc(".concat(height, "px + 46px + ").concat(token.marginLG, "px * 2)"),
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
            className: "pageWithFixedBlockCss tb-page-content",
            children: props.children
        })
    });
}
export { Page };
