import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { createContext, useEffect, useState } from "react";
import { RecursionField, observer, useFieldSchema } from "@tachybase/schema";
import { css } from "@emotion/css";
import { Avatar, List, Space } from "antd";
import { useSchemaInitializerRender, withDynamicSchemaProps } from "../../application/index.mjs";
import { CollectionContext, DataSourceContext } from "../../data-source/index.mjs";
import { Icon } from "../../icon/index.mjs";
import { DndContext, useDesignable } from "../../schema-component/index.mjs";
import { QuickAccessLayout } from "./blockSettings.mjs";
function _tagged_template_literal(strings, raw) {
    if (!raw) raw = strings.slice(0);
    return Object.freeze(Object.defineProperties(strings, {
        raw: {
            value: Object.freeze(raw)
        }
    }));
}
function _templateObject() {
    const data = _tagged_template_literal([
        "\n                    .ant-list-item-meta-avatar {\n                      margin-inline-end: 0px !important;\n                    }\n                    .ant-list-item-meta-title {\n                      overflow: hidden;\n                      text-overflow: ellipsis;\n                    }\n                    .ant-list-item-meta-title button {\n                      font-size: 16px;\n                      overflow: hidden;\n                      text-overflow: ellipsis;\n                      width: 100%;\n                      text-align: left;\n                    }\n                  "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const ConfigureActionsButton = observer(()=>{
    const fieldSchema = useFieldSchema();
    const { render } = useSchemaInitializerRender(fieldSchema['x-initializer']);
    return render();
}, {
    displayName: 'QuickAccessConfigureActionsButton'
});
const InternalIcons = ()=>{
    const fieldSchema = useFieldSchema();
    const { designable } = useDesignable();
    const { layout = QuickAccessLayout.Grid } = fieldSchema.parent['x-component-props'] || {};
    const [gap, setGap] = useState(8);
    useEffect(()=>{
        const calculateGap = ()=>{
            const container = document.getElementsByClassName('mobile-page-content')[0];
            if (container) {
                const containerWidth = container.offsetWidth - 48;
                const itemWidth = 100;
                const itemsPerRow = Math.floor(containerWidth / itemWidth);
                const totalItemWidth = itemsPerRow * itemWidth;
                const totalAvailableWidth = containerWidth;
                const totalGapsWidth = totalAvailableWidth - totalItemWidth;
                totalGapsWidth > 0 ? setGap(totalGapsWidth / (itemsPerRow - 1)) : setGap(0);
            }
        };
        window.addEventListener('resize', calculateGap);
        calculateGap();
        return ()=>{
            window.removeEventListener('resize', calculateGap);
        };
    }, [
        Object.keys((null == fieldSchema ? void 0 : fieldSchema.properties) || {}).length
    ]);
    return /*#__PURE__*/ jsx("div", {
        style: {
            marginBottom: designable ? '1rem' : 0
        },
        children: /*#__PURE__*/ jsx(DndContext, {
            children: layout === QuickAccessLayout.Grid ? /*#__PURE__*/ jsx(Space, {
                wrap: true,
                size: gap,
                children: fieldSchema.mapProperties((s, key)=>/*#__PURE__*/ jsx(RecursionField, {
                        name: key,
                        schema: s
                    }, key))
            }) : /*#__PURE__*/ jsx(List, {
                itemLayout: "horizontal",
                children: fieldSchema.mapProperties((s, key)=>{
                    var _s_xcomponentprops, _s_xcomponentprops1;
                    const icon = null == (_s_xcomponentprops = s['x-component-props']) ? void 0 : _s_xcomponentprops['icon'];
                    const backgroundColor = null == (_s_xcomponentprops1 = s['x-component-props']) ? void 0 : _s_xcomponentprops1['iconColor'];
                    return /*#__PURE__*/ jsx(List.Item, {
                        className: css(_templateObject()),
                        children: /*#__PURE__*/ jsx(List.Item.Meta, {
                            avatar: /*#__PURE__*/ jsx(Avatar, {
                                style: {
                                    backgroundColor
                                },
                                icon: /*#__PURE__*/ jsx(Icon, {
                                    type: icon
                                })
                            }),
                            title: /*#__PURE__*/ jsx(RecursionField, {
                                name: key,
                                schema: s
                            }, key)
                        })
                    }, key);
                })
            })
        })
    });
};
const QuickAccessBlockContext = /*#__PURE__*/ createContext({
    layout: 'grid'
});
const QuickAccessBlock = withDynamicSchemaProps((props)=>{
    const fieldSchema = useFieldSchema();
    const { layout = 'grid' } = fieldSchema['x-component-props'] || {};
    return /*#__PURE__*/ jsx(QuickAccessBlockContext.Provider, {
        value: {
            layout
        },
        children: /*#__PURE__*/ jsx(DataSourceContext.Provider, {
            value: void 0,
            children: /*#__PURE__*/ jsx(CollectionContext.Provider, {
                value: void 0,
                children: props.children
            })
        })
    });
}, {
    displayName: 'QuickAccessBlock'
});
QuickAccessBlock.ActionBar = ()=>/*#__PURE__*/ jsxs(Fragment, {
        children: [
            /*#__PURE__*/ jsx(InternalIcons, {}),
            /*#__PURE__*/ jsx(ConfigureActionsButton, {})
        ]
    });
export { QuickAccessBlock, QuickAccessBlockContext };
