import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo } from "react";
import { FormLayout } from "@tachybase/components";
import { FormContext, createForm, useField, useFieldSchema } from "@tachybase/schema";
import { withDynamicSchemaProps } from "../../../application/hoc/withDynamicSchemaProps.mjs";
import { BlockProvider, useBlockRequestContext } from "../../../block-provider/index.mjs";
import { useGridCardBlockParams } from "../../../modules/blocks/data-blocks/grid-card/hooks/useGridCardBlockParams.mjs";
import GridCard_Decorator_style from "./GridCard.Decorator.style.mjs";
const GridCardBlockContext = /*#__PURE__*/ createContext({});
GridCardBlockContext.displayName = 'GridCardBlockContext';
const InternalGridCardBlockProvider = (props)=>{
    var _service_data;
    const { resource, service } = useBlockRequestContext();
    const field = useField();
    const { wrapSSR, componentCls, hashId } = GridCard_Decorator_style();
    const form = useMemo(()=>createForm({
            readPretty: true
        }), []);
    useEffect(()=>{
        if (!(null == service ? void 0 : service.loading)) {
            var _service_data;
            form.setValuesIn(field.address.concat('list').toString(), null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.data);
        }
    }, [
        field.address,
        form,
        null == service ? void 0 : null == (_service_data = service.data) ? void 0 : _service_data.data,
        null == service ? void 0 : service.loading
    ]);
    return wrapSSR(/*#__PURE__*/ jsx(GridCardBlockContext.Provider, {
        value: {
            service,
            resource,
            columnCount: props.columnCount,
            needInfiniteScroll: props.needInfiniteScroll
        },
        children: /*#__PURE__*/ jsx(FormContext.Provider, {
            value: form,
            children: /*#__PURE__*/ jsx(FormLayout, {
                layout: 'vertical',
                children: /*#__PURE__*/ jsx("div", {
                    className: "".concat(componentCls, " ").concat(hashId),
                    children: props.children
                })
            })
        })
    }));
};
const useCompatGridCardBlockParams = (props)=>{
    const schema = useFieldSchema();
    if (schema['x-use-decorator-props']) return props.params;
    return useGridCardBlockParams(props);
};
const GridCardBlockProvider = withDynamicSchemaProps((props)=>{
    const params = useCompatGridCardBlockParams(props);
    return /*#__PURE__*/ jsx(BlockProvider, {
        name: "grid-card",
        ...props,
        params: params,
        children: /*#__PURE__*/ jsx(InternalGridCardBlockProvider, {
            ...props
        })
    });
});
const useGridCardBlockContext = ()=>useContext(GridCardBlockContext);
const useGridCardItemProps = ()=>({});
export { GridCardBlockContext, GridCardBlockProvider, useGridCardBlockContext, useGridCardItemProps };
