import { jsx, jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useState } from "react";
import { RecursionField, Schema, useField, useFieldSchema } from "@tachybase/schema";
import { Col, List } from "antd";
import { createStyles } from "antd-style";
import classnames from "classnames";
import { withDynamicSchemaProps } from "../../../application/hoc/withDynamicSchemaProps.mjs";
import { SortableItem } from "../../common/index.mjs";
import { InfiniteScroll } from "../../common/infinite-scroll/infinite-scroll.mjs";
import { SchemaComponentOptions } from "../../core/index.mjs";
import { useDesigner, useProps } from "../../hooks/index.mjs";
import { GridCardBlockProvider, useGridCardBlockContext, useGridCardItemProps } from "./GridCard.Decorator.mjs";
import { GridCardDesigner } from "./GridCard.Designer.mjs";
import { GridCardItem } from "./GridCard.Item.mjs";
import { useGridCardActionBarProps } from "./hooks.mjs";
import { defaultColumnCount, pageSizeOptions } from "./options.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      width: 100%;\n      &:hover {\n        > .general-schema-designer {\n          display: block;\n        }\n      }\n\n      > .general-schema-designer {\n        position: absolute;\n        z-index: 999;\n        top: 0;\n        bottom: 0;\n        left: 0;\n        right: 0;\n        display: none;\n        background: var(--colorBgSettingsHover);\n        border: 0;\n        pointer-events: none;\n        > .general-schema-designer-icons {\n          position: absolute;\n          right: 2px;\n          top: 2px;\n          line-height: 16px;\n          pointer-events: all;\n          .ant-space-item {\n            background-color: var(--colorSettings);\n            color: #fff;\n            line-height: 16px;\n            width: 16px;\n            padding-left: 1px;\n            align-self: stretch;\n          }\n        }\n      }\n    "
    ]);
    _templateObject = function() {
        return data;
    };
    return data;
}
const rowGutter = {
    md: 12,
    sm: 5,
    xs: 5
};
const useStyles = createStyles((param)=>{
    let { css } = param;
    return {
        designer: css(_templateObject())
    };
});
const InternalGridCard = (props)=>{
    var _service_data;
    const { columnCount: columnCountProp, pagination } = useProps(props);
    const { styles } = useStyles();
    const { service, columnCount: _columnCount = defaultColumnCount, needInfiniteScroll } = useGridCardBlockContext();
    const columnCount = columnCountProp || _columnCount;
    const { run, params } = service;
    const meta = null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.meta;
    const fieldSchema = useFieldSchema();
    const field = useField();
    const Designer = useDesigner();
    const [schemaMap] = useState(new Map());
    const getSchema = useCallback((key)=>{
        if (!schemaMap.has(key)) schemaMap.set(key, new Schema({
            type: 'object',
            properties: {
                [key]: {
                    ...fieldSchema.properties['item']
                }
            }
        }));
        return schemaMap.get(key);
    }, [
        fieldSchema.properties,
        schemaMap
    ]);
    const onPaginationChange = useCallback((page, pageSize)=>{
        run({
            ...null == params ? void 0 : params[0],
            page: page,
            pageSize: pageSize
        });
    }, [
        run,
        params
    ]);
    const [hasMore, setHasMore] = useState(true);
    const [fetchCount, setFetchCount] = useState(1);
    const loadMore = useCallback(async ()=>{
        const { pageSize = 5 } = meta || {};
        await onPaginationChange(1, fetchCount * pageSize);
        setFetchCount((prevFetchCount)=>prevFetchCount + 1);
        setHasMore(false);
    }, [
        meta,
        onPaginationChange,
        setHasMore
    ]);
    useEffect(()=>{
        const { count, pageSize = 10, page = 1 } = meta || {};
        const hasMore = count > page * pageSize;
        setHasMore(hasMore);
    }, [
        null == meta ? void 0 : meta.pageSize
    ]);
    return /*#__PURE__*/ jsx(SchemaComponentOptions, {
        scope: {
            useGridCardItemProps: useGridCardItemProps,
            useGridCardActionBarProps: useGridCardActionBarProps
        },
        children: /*#__PURE__*/ jsxs(SortableItem, {
            className: classnames('tb-card-list', styles.designer),
            children: [
                /*#__PURE__*/ jsx(List, {
                    pagination: !meta || meta.count <= meta.pageSize || needInfiniteScroll ? false : {
                        ...pagination,
                        onChange: onPaginationChange,
                        total: (null == meta ? void 0 : meta.count) || 0,
                        pageSize: (null == meta ? void 0 : meta.pageSize) || 10,
                        current: (null == meta ? void 0 : meta.page) || 1,
                        pageSizeOptions: pageSizeOptions
                    },
                    dataSource: field.value,
                    grid: {
                        ...columnCount,
                        sm: columnCount.xs,
                        xl: columnCount.lg,
                        gutter: [
                            rowGutter,
                            rowGutter
                        ]
                    },
                    renderItem: (item, index)=>{
                        const schema = getSchema(index);
                        return /*#__PURE__*/ jsx(Col, {
                            style: {
                                height: '100%'
                            },
                            children: /*#__PURE__*/ jsx(RecursionField, {
                                basePath: field.address,
                                name: index,
                                onlyRenderProperties: true,
                                schema: schema
                            }, index)
                        });
                    },
                    loading: null == service ? void 0 : service.loading
                }),
                needInfiniteScroll && /*#__PURE__*/ jsx(InfiniteScroll, {
                    loadMore: loadMore,
                    hasMore: hasMore
                }),
                /*#__PURE__*/ jsx(Designer, {})
            ]
        })
    });
};
const GridCard = withDynamicSchemaProps(InternalGridCard);
GridCard.Item = GridCardItem;
GridCard.Designer = GridCardDesigner;
GridCard.Decorator = GridCardBlockProvider;
export { GridCard };
