"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    Page: ()=>Page
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const components_namespaceObject = require("@tachybase/components");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
const pro_layout_namespaceObject = require("@ant-design/pro-layout");
const external_antd_namespaceObject = require("antd");
const external_classnames_namespaceObject = require("classnames");
var external_classnames_default = /*#__PURE__*/ __webpack_require__.n(external_classnames_namespaceObject);
const external_react_error_boundary_namespaceObject = require("react-error-boundary");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_react_router_dom_namespaceObject = require("react-router-dom");
const external_index_js_namespaceObject = require("../index.js");
const index_js_namespaceObject = require("../__builtins__/index.js");
const style_js_namespaceObject = require("../../../built-in/acl/style.js");
const useContextMenu_js_namespaceObject = require("../../../built-in/context-menu/useContextMenu.js");
const document_title_index_js_namespaceObject = require("../../../built-in/document-title/index.js");
const FilterProvider_js_namespaceObject = require("../../../filter-provider/FilterProvider.js");
const external_icon_index_js_namespaceObject = require("../../../icon/index.js");
const useGetAriaLabelOfSchemaInitializer_js_namespaceObject = require("../../../schema-initializer/hooks/useGetAriaLabelOfSchemaInitializer.js");
const theme_index_js_namespaceObject = require("../../../style/theme/index.js");
const external_common_index_js_namespaceObject = require("../../common/index.js");
const sortable_item_index_js_namespaceObject = require("../../common/sortable-item/index.js");
const external_core_index_js_namespaceObject = require("../../core/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_error_fallback_index_js_namespaceObject = require("../error-fallback/index.js");
const external_FixedBlock_js_namespaceObject = require("./FixedBlock.js");
var external_FixedBlock_js_default = /*#__PURE__*/ __webpack_require__.n(external_FixedBlock_js_namespaceObject);
const external_PageTabDesigner_js_namespaceObject = require("./PageTabDesigner.js");
const external_style_js_namespaceObject = require("./style.js");
const Page = (props)=>{
    var _fieldSchema_xcomponentprops, _fieldSchema_xcomponentprops1, _fieldSchema_xcomponentprops2;
    const { children, ...others } = props;
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const compile = (0, external_hooks_index_js_namespaceObject.useCompile)();
    const { title, setTitle } = (0, document_title_index_js_namespaceObject.useDocumentTitle)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const dn = (0, external_hooks_index_js_namespaceObject.useDesignable)();
    const { theme } = (0, theme_index_js_namespaceObject.useGlobalTheme)();
    const { getAriaLabel } = (0, useGetAriaLabelOfSchemaInitializer_js_namespaceObject.useGetAriaLabelOfSchemaInitializer)();
    const { hiddenScrollArea } = (0, useContextMenu_js_namespaceObject.useContextMenu)();
    const [hasMounted, setHasMounted] = (0, external_react_namespaceObject.useState)(false);
    (0, external_react_namespaceObject.useEffect)(()=>{
        setTimeout(()=>{
            setHasMounted(true);
        });
    }, []);
    (0, external_react_namespaceObject.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, external_react_namespaceObject.useContext)(schema_namespaceObject.SchemaOptionsContext);
    const [searchParams, setSearchParams] = (0, external_react_router_dom_namespaceObject.useSearchParams)();
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const activeKey = (0, external_react_namespaceObject.useMemo)(()=>searchParams.get('tab') || Object.keys(fieldSchema.properties || {}).shift(), [
        fieldSchema.properties,
        searchParams
    ]);
    const [height, setHeight] = (0, external_react_namespaceObject.useState)(0);
    const { wrapSSR, hashId, componentCls } = (0, external_style_js_namespaceObject.useStyles)();
    const aclStyles = (0, style_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(FilterProvider_js_namespaceObject.FilterBlockProvider, {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            className: "".concat(componentCls, " ").concat(hashId, " ").concat(aclStyles.styles),
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_PageTabDesigner_js_namespaceObject.PageDesigner, {
                    title: fieldSchema.title || title
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(pro_layout_namespaceObject.PageHeader, {
                        className: external_classnames_default()('pageHeaderCss', pageHeaderTitle || enablePageTabs ? '' : 'height0'),
                        ghost: false,
                        title: pageHeaderTitle || ' ',
                        ...others,
                        footer: enablePageTabs && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_common_index_js_namespaceObject.DndContext, {
                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Tabs, {
                                size: 'small',
                                animated: hasMounted,
                                activeKey: activeKey,
                                onTabClick: (activeKey)=>{
                                    setLoading(true);
                                    setSearchParams([
                                        [
                                            'tab',
                                            activeKey
                                        ]
                                    ]);
                                    setTimeout(()=>{
                                        setLoading(false);
                                    }, 50);
                                },
                                tabBarExtraContent: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                                    className: "tb-tabs-wrapper",
                                    children: [
                                        !hiddenScrollArea && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.ScrollArea, {}),
                                        dn.designable && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Button, {
                                            "aria-label": getAriaLabel('tabs'),
                                            icon: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {}),
                                            className: 'addTabBtn',
                                            type: 'dashed',
                                            onClick: async ()=>{
                                                const values = await (0, external_index_js_namespaceObject.FormDialog)(t('Add tab'), ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_core_index_js_namespaceObject.SchemaComponentOptions, {
                                                        scope: options.scope,
                                                        components: {
                                                            ...options.components
                                                        },
                                                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(components_namespaceObject.FormLayout, {
                                                            layout: 'vertical',
                                                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_core_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)(sortable_item_index_js_namespaceObject.SortableItem, {
                                            id: schema.name,
                                            schema: schema,
                                            className: external_classnames_default()('tb-action-link', 'designerCss', props.className),
                                            children: [
                                                schema['x-icon'] && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_icon_index_js_namespaceObject.Icon, {
                                                    style: {
                                                        marginRight: 8
                                                    },
                                                    type: schema['x-icon']
                                                }),
                                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                                    children: schema.title || t('Unnamed')
                                                }),
                                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_PageTabDesigner_js_namespaceObject.PageTabDesigner, {
                                                    schema: schema
                                                })
                                            ]
                                        }),
                                        key: schema.name
                                    }))
                            })
                        }),
                        extra: !enablePageTabs && !hiddenScrollArea && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject.ScrollArea, {})
                    })
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    className: "tb-page-wrapper",
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_error_boundary_namespaceObject.ErrorBoundary, {
                        FallbackComponent: external_error_fallback_index_js_namespaceObject.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, index_js_namespaceObject.useToken)();
    if (loading) return;
    return !disablePageHeader && enablePageTabs ? fieldSchema.mapProperties((schema)=>{
        if (schema.name !== activeKey) return null;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_FixedBlock_js_default(), {
            height: "calc(".concat(height, "px + 46px + ").concat(token.marginLG, "px * 2)"),
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_core_index_js_namespaceObject.SchemaComponent, {
                schema: new schema_namespaceObject.Schema({
                    properties: {
                        [schema.name]: schema
                    }
                })
            })
        }, schema.name);
    }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_FixedBlock_js_default(), {
        height: "calc(".concat(height, "px + 46px + ").concat(token.marginLG, "px * 2)"),
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            className: "pageWithFixedBlockCss tb-page-content",
            children: props.children
        })
    });
}
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
