import { jsx, jsxs } from "react/jsx-runtime";
import { useCallback, useState } from "react";
import { RecursionField, Schema, useField, useFieldSchema } from "@tachybase/schema";
import { List } from "antd";
import classnames from "classnames";
import { withDynamicSchemaProps } from "../../../application/hoc/withDynamicSchemaProps.mjs";
import { SortableItem } from "../../common/index.mjs";
import { SchemaComponentOptions } from "../../core/index.mjs";
import { useDesigner } from "../../hooks/index.mjs";
import { useListActionBarProps } from "./hooks.mjs";
import { ListBlockProvider, useListBlockContext, useListItemProps } from "./List.Decorator.mjs";
import { ListDesigner } from "./List.Designer.mjs";
import { ListItem } from "./List.Item.mjs";
import List_style from "./List.style.mjs";
const InternalList = (props)=>{
    var _service_data, _field_value;
    const { service } = useListBlockContext();
    const { run, params } = service;
    const fieldSchema = useFieldSchema();
    const Designer = useDesigner();
    const meta = null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.meta;
    const field = useField();
    const [schemaMap] = useState(new Map());
    const { wrapSSR, componentCls, hashId } = List_style();
    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
    ]);
    return wrapSSR(/*#__PURE__*/ jsx(SchemaComponentOptions, {
        scope: {
            useListItemProps: useListItemProps,
            useListActionBarProps: useListActionBarProps
        },
        children: /*#__PURE__*/ jsxs(SortableItem, {
            className: classnames('tb-list', componentCls, hashId),
            children: [
                /*#__PURE__*/ jsx(List, {
                    ...props,
                    pagination: !meta || meta.count <= meta.pageSize ? false : {
                        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
                    },
                    loading: null == service ? void 0 : service.loading,
                    children: (null == (_field_value = field.value) ? void 0 : _field_value.length) ? field.value.map((item, index)=>/*#__PURE__*/ jsx(RecursionField, {
                            basePath: field.address,
                            name: index,
                            onlyRenderProperties: true,
                            schema: getSchema(index)
                        }, index)) : null
                }),
                /*#__PURE__*/ jsx(Designer, {})
            ]
        })
    }));
};
const List_List = withDynamicSchemaProps(InternalList);
List_List.Item = ListItem;
List_List.Designer = ListDesigner;
List_List.Decorator = ListBlockProvider;
export { List_List as List };
