import { jsx } from "react/jsx-runtime";
import { useCallback } from "react";
import { TableOutlined } from "@ant-design/icons";
import { useSchemaInitializer, useSchemaInitializerItem } from "../../../../application/index.mjs";
import { useCollectionManager } from "../../../../data-source/index.mjs";
import { DataBlockInitializer } from "../../../../schema-initializer/items/DataBlockInitializer.mjs";
import { createDetailsWithPaginationUISchema } from "./createDetailsWithPaginationUISchema.mjs";
const DetailsBlockInitializer = (param)=>{
    let { filterCollections, onlyCurrentDataSource, hideSearch, componentType = 'Details', createBlockSchema, templateWrap, showAssociationFields, hideChildrenIfSingleCollection } = param;
    const itemConfig = useSchemaInitializerItem();
    const { createDetailsBlock } = useCreateDetailsBlock();
    return /*#__PURE__*/ jsx(DataBlockInitializer, {
        ...itemConfig,
        icon: /*#__PURE__*/ jsx(TableOutlined, {}),
        componentType: componentType,
        onCreateBlockSchema: async (options)=>{
            if (createBlockSchema) return createBlockSchema(options);
            createDetailsBlock(options);
        },
        onlyCurrentDataSource: !!onlyCurrentDataSource,
        hideSearch: hideSearch,
        filter: filterCollections,
        templateWrap: templateWrap,
        showAssociationFields: showAssociationFields,
        hideChildrenIfSingleCollection: hideChildrenIfSingleCollection
    });
};
const useCreateDetailsBlock = ()=>{
    const { insert } = useSchemaInitializer();
    const cm = useCollectionManager();
    const createDetailsBlock = useCallback((param)=>{
        let { item } = param;
        const collection = cm.getCollection(item.name);
        const schema = createDetailsWithPaginationUISchema({
            collectionName: item.name,
            dataSource: item.dataSource,
            rowKey: collection.filterTargetKey || 'id',
            hideActionInitializer: !(('view' !== collection.template || (null == collection ? void 0 : collection.writableView)) && 'sql' !== collection.template)
        });
        insert(schema);
    }, [
        cm,
        insert
    ]);
    return {
        createDetailsBlock
    };
};
export { DetailsBlockInitializer, useCreateDetailsBlock };
