"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__, {
    GeneralSchemaDesigner: ()=>GeneralSchemaDesigner,
    SchemaToolbar: ()=>SchemaToolbar
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const icons_namespaceObject = require("@ant-design/icons");
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_i18next_namespaceObject = require("react-i18next");
const index_js_namespaceObject = require("../application/index.js");
const DataSourceManagerProvider_js_namespaceObject = require("../data-source/data-source/DataSourceManagerProvider.js");
const DataSourceProvider_js_namespaceObject = require("../data-source/data-source/DataSourceProvider.js");
const external_schema_component_index_js_namespaceObject = require("../schema-component/index.js");
const utils_js_namespaceObject = require("../schema-initializer/utils.js");
const useGetAriaLabelOfDesigner_js_namespaceObject = require("./hooks/useGetAriaLabelOfDesigner.js");
const external_SchemaSettings_js_namespaceObject = require("./SchemaSettings.js");
const external_styles_js_namespaceObject = require("./styles.js");
const GeneralSchemaDesigner = (props)=>{
    var _rowCtx_cols, _ctx_renderSchemaInitializer;
    const { disableInitializer, title, template, schemaSettings, contextValue, draggable = true, showDataSource = true, AddMenuModalComponent } = props;
    const { dn, designable } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    const field = (0, schema_namespaceObject.useField)();
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { getAriaLabel } = (0, useGetAriaLabelOfDesigner_js_namespaceObject.useGetAriaLabelOfDesigner)();
    const schemaSettingsProps = {
        dn,
        field,
        fieldSchema
    };
    const { render: schemaSettingsRender, exists: schemaSettingsExists } = (0, index_js_namespaceObject.useSchemaSettingsRender)(fieldSchema['x-settings'] || schemaSettings, fieldSchema['x-settings-props']);
    const rowCtx = (0, external_schema_component_index_js_namespaceObject.useGridRowContext)();
    const ctx = (0, external_schema_component_index_js_namespaceObject.useGridContext)();
    const { styles } = (0, external_styles_js_namespaceObject.useStyles)();
    const dm = (0, DataSourceManagerProvider_js_namespaceObject.useDataSourceManager)();
    const dataSources = null == dm ? void 0 : dm.getDataSources();
    const dataSourceContext = (0, DataSourceProvider_js_namespaceObject.useDataSource)();
    const dataSource = (null == dataSources ? void 0 : dataSources.length) > 1 && dataSourceContext;
    const templateName = [
        'FormItem',
        'ReadPrettyFormItem'
    ].includes(null == template ? void 0 : template.componentName) ? "".concat(null == template ? void 0 : template.name, " ").concat(t('(Fields only)')) : null == template ? void 0 : template.name;
    const initializerProps = (0, external_react_namespaceObject.useMemo)(()=>{
        var _rowCtx_cols;
        return {
            insertPosition: 'afterEnd',
            wrap: (null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length) > 1 ? void 0 : utils_js_namespaceObject.gridRowColWrap,
            Component: (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {
                    ...props,
                    role: "button",
                    "aria-label": getAriaLabel('schema-initializer'),
                    style: {
                        cursor: 'pointer',
                        fontSize: 14
                    }
                })
        };
    }, [
        getAriaLabel,
        null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length
    ]);
    if (!designable) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.SchemaToolbarProvider, {
        ...contextValue,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
            className: external_classnames_default()('general-schema-designer', styles.overrideAntdCSS),
            children: [
                title && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    className: external_classnames_default()('general-schema-designer-title', styles.titleCss),
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space, {
                        size: 2,
                        children: [
                            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                className: 'title-tag',
                                children: showDataSource && dataSource ? "".concat(compile(null == dataSource ? void 0 : dataSource.displayName), " > ").concat(compile(title)) : compile(title)
                            }),
                            template && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
                                className: 'title-tag',
                                children: [
                                    t('Reference template'),
                                    ": ",
                                    templateName || t('Untitled')
                                ]
                            })
                        ]
                    })
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    className: 'general-schema-designer-icons',
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space, {
                        size: 3,
                        align: 'center',
                        children: [
                            !!AddMenuModalComponent && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_SchemaSettings_js_namespaceObject.SchemaSettingsDropdown, {
                                title: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {
                                    role: "button",
                                    "aria-label": getAriaLabel('add-menu')
                                }),
                                ...schemaSettingsProps,
                                children: AddMenuModalComponent
                            }),
                            draggable && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.DragHandler, {
                                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DragOutlined, {
                                    role: "button",
                                    "aria-label": getAriaLabel('drag-handler')
                                })
                            }),
                            !disableInitializer && ((null == ctx ? void 0 : ctx.InitializerComponent) ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ctx.InitializerComponent, {
                                ...initializerProps
                            }) : null == ctx ? void 0 : null === (_ctx_renderSchemaInitializer = ctx.renderSchemaInitializer) || void 0 === _ctx_renderSchemaInitializer ? void 0 : _ctx_renderSchemaInitializer.call(ctx, initializerProps)),
                            schemaSettingsExists ? schemaSettingsRender(contextValue) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_SchemaSettings_js_namespaceObject.SchemaSettingsDropdown, {
                                title: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.MenuOutlined, {
                                    role: "button",
                                    "aria-label": getAriaLabel('schema-settings'),
                                    style: {
                                        cursor: 'pointer',
                                        fontSize: 12
                                    }
                                }),
                                ...schemaSettingsProps,
                                children: props.children
                            })
                        ]
                    })
                })
            ]
        })
    });
};
const InternalSchemaToolbar = (props)=>{
    var _rowCtx_cols;
    const { title, initializer, settings, showBackground, showBorder = true, draggable = true } = props;
    const { designable } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    const fieldSchema = (0, schema_namespaceObject.useFieldSchema)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const { styles } = (0, external_styles_js_namespaceObject.useStyles)();
    const { getAriaLabel } = (0, useGetAriaLabelOfDesigner_js_namespaceObject.useGetAriaLabelOfDesigner)();
    const dm = (0, DataSourceManagerProvider_js_namespaceObject.useDataSourceManager)();
    const dataSources = null == dm ? void 0 : dm.getDataSources();
    const dataSourceContext = (0, DataSourceProvider_js_namespaceObject.useDataSource)();
    const dataSource = (null == dataSources ? void 0 : dataSources.length) > 1 && dataSourceContext;
    const titleArr = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!title) return;
        if ('string' == typeof title) return [
            compile(title)
        ];
        if (Array.isArray(title)) return title.map((item)=>compile(item));
    }, [
        compile,
        title
    ]);
    const { render: schemaSettingsRender, exists: schemaSettingsExists } = (0, index_js_namespaceObject.useSchemaSettingsRender)(fieldSchema['x-settings'] || settings, fieldSchema['x-settings-props']);
    const { render: schemaInitializerRender, exists: schemaInitializerExists } = (0, index_js_namespaceObject.useSchemaInitializerRender)(fieldSchema['x-initializer'] || initializer, fieldSchema['x-initializer-props']);
    const rowCtx = (0, external_schema_component_index_js_namespaceObject.useGridRowContext)();
    const gridContext = (0, external_schema_component_index_js_namespaceObject.useGridContext)();
    const initializerProps = (0, external_react_namespaceObject.useMemo)(()=>{
        var _rowCtx_cols;
        return {
            insertPosition: 'afterEnd',
            wrap: (null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length) > 1 ? void 0 : utils_js_namespaceObject.gridRowColWrap,
            Component: (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.PlusOutlined, {
                    ...props,
                    role: "button",
                    "aria-label": getAriaLabel('schema-initializer'),
                    style: {
                        cursor: 'pointer',
                        fontSize: 14
                    }
                })
        };
    }, [
        getAriaLabel,
        null == rowCtx ? void 0 : null === (_rowCtx_cols = rowCtx.cols) || void 0 === _rowCtx_cols ? void 0 : _rowCtx_cols.length
    ]);
    const dragElement = (0, external_react_namespaceObject.useMemo)(()=>{
        if (false === draggable) return null;
        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.DragHandler, {
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icons_namespaceObject.DragOutlined, {
                role: "button",
                "aria-label": getAriaLabel('drag-handler')
            })
        });
    }, [
        draggable,
        getAriaLabel
    ]);
    const initializerElement = (0, external_react_namespaceObject.useMemo)(()=>{
        if (false === initializer) return null;
        if ((null == gridContext ? void 0 : gridContext.InitializerComponent) || (null == gridContext ? void 0 : gridContext.renderSchemaInitializer)) {
            var _gridContext_renderSchemaInitializer;
            return (null == gridContext ? void 0 : gridContext.InitializerComponent) ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(gridContext.InitializerComponent, {
                ...initializerProps
            }) : null === (_gridContext_renderSchemaInitializer = gridContext.renderSchemaInitializer) || void 0 === _gridContext_renderSchemaInitializer ? void 0 : _gridContext_renderSchemaInitializer.call(gridContext, initializerProps);
        }
        if (!schemaInitializerExists) return null;
        return schemaInitializerRender(initializerProps);
    }, [
        gridContext,
        initializer,
        initializerProps,
        schemaInitializerExists,
        schemaInitializerRender
    ]);
    const settingsElement = (0, external_react_namespaceObject.useMemo)(()=>false !== settings && schemaSettingsExists ? schemaSettingsRender() : null, [
        schemaSettingsExists,
        schemaSettingsRender,
        settings
    ]);
    const toolbarRef = (0, external_react_namespaceObject.useRef)(null);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const toolbarElement = toolbarRef.current;
        const parentElement = null == toolbarElement ? void 0 : toolbarElement.parentElement;
        function show() {
            if (toolbarElement) toolbarElement.style.display = 'block';
        }
        function hide() {
            if (toolbarElement) toolbarElement.style.display = 'none';
        }
        if (parentElement) {
            const style = window.getComputedStyle(parentElement);
            if ('static' === style.position) parentElement.style.position = 'relative';
            parentElement.addEventListener('mouseenter', show);
            parentElement.addEventListener('mouseleave', hide);
        }
        return ()=>{
            if (parentElement) {
                parentElement.removeEventListener('mouseenter', show);
                parentElement.removeEventListener('mouseleave', hide);
            }
        };
    }, []);
    if (!designable) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        ref: toolbarRef,
        className: styles.toolbar,
        style: {
            border: showBorder ? 'auto' : 0,
            background: showBackground ? 'auto' : 0
        },
        children: [
            titleArr && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: styles.toolbarTitle,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Space, {
                    size: 2,
                    children: titleArr.map((item)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                            className: styles.toolbarTitleTag,
                            children: dataSource ? "".concat(compile(null == dataSource ? void 0 : dataSource.displayName), " > ").concat(item) : item
                        }, item))
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: styles.toolbarIcons,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Space, {
                    size: 3,
                    align: 'center',
                    children: [
                        dragElement,
                        initializerElement,
                        settingsElement
                    ]
                })
            })
        ]
    });
};
const SchemaToolbar = (props)=>{
    const { designable } = (0, external_schema_component_index_js_namespaceObject.useDesignable)();
    if (!designable) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalSchemaToolbar, {
        ...props
    });
};
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
});
