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_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_antd_style__ from "antd-style";
import * as __WEBPACK_EXTERNAL_MODULE_classnames__ from "classnames";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../../../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_index_mjs__ from "../../common/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__common_infinite_scroll_infinite_scroll_mjs__ from "../../common/infinite-scroll/infinite-scroll.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__GridCard_Decorator_mjs__ from "./GridCard.Decorator.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__GridCard_Designer_mjs__ from "./GridCard.Designer.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__GridCard_Item_mjs__ from "./GridCard.Item.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_mjs__ from "./hooks.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__options_mjs__ 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 = (0, __WEBPACK_EXTERNAL_MODULE_antd_style__.createStyles)((param)=>{
    let { css } = param;
    return {
        designer: css(_templateObject())
    };
});
const InternalGridCard = (props)=>{
    var _service_data;
    const { columnCount: columnCountProp, pagination } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useProps)(props);
    const { styles } = useStyles();
    const { service, columnCount: _columnCount = __WEBPACK_EXTERNAL_MODULE__options_mjs__.defaultColumnCount, needInfiniteScroll } = (0, __WEBPACK_EXTERNAL_MODULE__GridCard_Decorator_mjs__.useGridCardBlockContext)();
    const columnCount = columnCountProp || _columnCount;
    const { run, params } = service;
    const meta = null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.meta;
    const fieldSchema = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useFieldSchema)();
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const Designer = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useDesigner)();
    const [schemaMap] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(new Map());
    const getSchema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((key)=>{
        if (!schemaMap.has(key)) schemaMap.set(key, new __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema({
            type: 'object',
            properties: {
                [key]: {
                    ...fieldSchema.properties['item']
                }
            }
        }));
        return schemaMap.get(key);
    }, [
        fieldSchema.properties,
        schemaMap
    ]);
    const onPaginationChange = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((page, pageSize)=>{
        run({
            ...null == params ? void 0 : params[0],
            page: page,
            pageSize: pageSize
        });
    }, [
        run,
        params
    ]);
    const [hasMore, setHasMore] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
    const [fetchCount, setFetchCount] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(1);
    const loadMore = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)(async ()=>{
        const { pageSize = 5 } = meta || {};
        await onPaginationChange(1, fetchCount * pageSize);
        setFetchCount((prevFetchCount)=>prevFetchCount + 1);
        setHasMore(false);
    }, [
        meta,
        onPaginationChange,
        setHasMore
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const { count, pageSize = 10, page = 1 } = meta || {};
        const hasMore = count > page * pageSize;
        setHasMore(hasMore);
    }, [
        null == meta ? void 0 : meta.pageSize
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__core_index_mjs__.SchemaComponentOptions, {
        scope: {
            useGridCardItemProps: __WEBPACK_EXTERNAL_MODULE__GridCard_Decorator_mjs__.useGridCardItemProps,
            useGridCardActionBarProps: __WEBPACK_EXTERNAL_MODULE__hooks_mjs__.useGridCardActionBarProps
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__common_index_mjs__.SortableItem, {
            className: (0, __WEBPACK_EXTERNAL_MODULE_classnames__["default"])('tb-card-list', styles.designer),
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.List, {
                    pagination: !!meta && !(meta.count <= meta.pageSize) && !needInfiniteScroll && {
                        ...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: __WEBPACK_EXTERNAL_MODULE__options_mjs__.pageSizeOptions
                    },
                    dataSource: field.value,
                    grid: {
                        ...columnCount,
                        sm: columnCount.xs,
                        xl: columnCount.lg,
                        gutter: [
                            rowGutter,
                            rowGutter
                        ]
                    },
                    renderItem: (item, index)=>{
                        const schema = getSchema(index);
                        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Col, {
                            style: {
                                height: '100%'
                            },
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.RecursionField, {
                                basePath: field.address,
                                name: index,
                                onlyRenderProperties: true,
                                schema: schema
                            }, index)
                        });
                    },
                    loading: null == service ? void 0 : service.loading
                }),
                needInfiniteScroll && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__common_infinite_scroll_infinite_scroll_mjs__.InfiniteScroll, {
                    loadMore: loadMore,
                    hasMore: hasMore
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Designer, {})
            ]
        })
    });
};
const GridCard = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)(InternalGridCard);
GridCard.Item = __WEBPACK_EXTERNAL_MODULE__GridCard_Item_mjs__.GridCardItem;
GridCard.Designer = __WEBPACK_EXTERNAL_MODULE__GridCard_Designer_mjs__.GridCardDesigner;
GridCard.Decorator = __WEBPACK_EXTERNAL_MODULE__GridCard_Decorator_mjs__.GridCardBlockProvider;
export { GridCard };
