import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo, useRef } from "react";
import { createForm, useField } from "@tachybase/schema";
import { Spin } from "antd";
import lodash from "lodash";
import { withDynamicSchemaProps } from "../application/hoc/withDynamicSchemaProps.mjs";
import { useCollectionParentRecord } from "../data-source/collection-record/CollectionRecordProvider.mjs";
import { RecordProvider } from "../record-provider/index.mjs";
import { BlockProvider, useBlockRequestContext } from "./BlockProvider.mjs";
import { useParsedFilter } from "./hooks/index.mjs";
const DetailsBlockContext = /*#__PURE__*/ createContext({});
DetailsBlockContext.displayName = 'DetailsBlockContext';
const InternalDetailsBlockProvider = (props)=>{
    var _service_data_data, _service_data, _service_data1, _service_params_, _service_params;
    const { action, readPretty } = props;
    const field = useField();
    const form = useMemo(()=>createForm({
            readPretty
        }), [
        readPretty
    ]);
    const { resource, service } = useBlockRequestContext();
    const parentRecord = useCollectionParentRecord();
    const currentRecord = ('list' === action ? null == service ? void 0 : null == (_service_data = service.data) ? void 0 : null == (_service_data_data = _service_data.data) ? void 0 : _service_data_data[0] : null == service ? void 0 : null == (_service_data1 = service.data) ? void 0 : _service_data1.data) || {};
    const formBlockRef = useRef();
    const detailsBLockValue = useMemo(()=>({
            action,
            form,
            field,
            service,
            resource,
            formBlockRef
        }), [
        action,
        field,
        form,
        resource,
        service
    ]);
    const { filter } = useParsedFilter({
        filterOption: null == service ? void 0 : null == (_service_params = service.params) ? void 0 : null == (_service_params_ = _service_params[0]) ? void 0 : _service_params_.filter
    });
    useEffect(()=>{
        if (!lodash.isEmpty(filter) && !service.loading) {
            var _service_params;
            null == service || service.run({
                ...null == service ? void 0 : null == (_service_params = service.params) ? void 0 : _service_params[0],
                filter
            });
        }
    }, [
        JSON.stringify(filter)
    ]);
    if (service.loading && !field.loaded) return /*#__PURE__*/ jsx(Spin, {});
    field.loaded = true;
    return /*#__PURE__*/ jsx(DetailsBlockContext.Provider, {
        value: detailsBLockValue,
        children: /*#__PURE__*/ jsx("div", {
            ref: formBlockRef,
            children: /*#__PURE__*/ jsx(RecordProvider, {
                isNew: false,
                record: currentRecord,
                parent: null == parentRecord ? void 0 : parentRecord.data,
                children: props.children
            })
        })
    });
};
const DetailsBlockProvider = withDynamicSchemaProps((props)=>/*#__PURE__*/ jsx(BlockProvider, {
        name: "details",
        ...props,
        children: /*#__PURE__*/ jsx(InternalDetailsBlockProvider, {
            ...props
        })
    }));
const useDetailsBlockContext = ()=>useContext(DetailsBlockContext);
const useDetailsBlockProps = ()=>{
    var _ctx_service_data, _ctx_service;
    const ctx = useDetailsBlockContext();
    useEffect(()=>{
        if (!ctx.service.loading) {
            var _ctx_service_data_data, _ctx_service_data, _ctx_service, _ctx_service_data1, _ctx_service1;
            const data = 'list' === ctx.action ? null == (_ctx_service = ctx.service) ? void 0 : null == (_ctx_service_data = _ctx_service.data) ? void 0 : null == (_ctx_service_data_data = _ctx_service_data.data) ? void 0 : _ctx_service_data_data[0] : null == (_ctx_service1 = ctx.service) ? void 0 : null == (_ctx_service_data1 = _ctx_service1.data) ? void 0 : _ctx_service_data1.data;
            ctx.form.reset().then(()=>{
                ctx.form.setValues(data || {});
            }).catch(console.error);
        }
    }, [
        ctx.action,
        ctx.form,
        null == (_ctx_service = ctx.service) ? void 0 : null == (_ctx_service_data = _ctx_service.data) ? void 0 : _ctx_service_data.data,
        ctx.service.loading
    ]);
    return {
        form: ctx.form
    };
};
export { DetailsBlockContext, DetailsBlockProvider, useDetailsBlockContext, useDetailsBlockProps };
