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_lodash__ from "lodash";
import * as __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__ from "../application/hoc/withDynamicSchemaProps.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__ from "../data-source/collection-record/CollectionRecordProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../record-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__ from "./BlockProvider.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "./hooks/index.mjs";
const DetailsBlockContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.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 = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>(0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.createForm)({
            readPretty
        }), [
        readPretty
    ]);
    const { resource, service } = (0, __WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.useBlockRequestContext)();
    const parentRecord = (0, __WEBPACK_EXTERNAL_MODULE__data_source_collection_record_CollectionRecordProvider_mjs__.useCollectionParentRecord)();
    const currentRecord = ('list' === action ? null == service ? void 0 : null === (_service_data = service.data) || void 0 === _service_data ? void 0 : null === (_service_data_data = _service_data.data) || void 0 === _service_data_data ? void 0 : _service_data_data[0] : null == service ? void 0 : null === (_service_data1 = service.data) || void 0 === _service_data1 ? void 0 : _service_data1.data) || {};
    const formBlockRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    const detailsBLockValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>({
            action,
            form,
            field,
            service,
            resource,
            formBlockRef
        }), [
        action,
        field,
        form,
        resource,
        service
    ]);
    const { filter } = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useParsedFilter)({
        filterOption: null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : null === (_service_params_ = _service_params[0]) || void 0 === _service_params_ ? void 0 : _service_params_.filter
    });
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (!__WEBPACK_EXTERNAL_MODULE_lodash__["default"].isEmpty(filter) && !service.loading) {
            var _service_params;
            null == service || service.run({
                ...null == service ? void 0 : null === (_service_params = service.params) || void 0 === _service_params ? void 0 : _service_params[0],
                filter
            });
        }
    }, [
        JSON.stringify(filter)
    ]);
    if (service.loading && !field.loaded) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {});
    field.loaded = true;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DetailsBlockContext.Provider, {
        value: detailsBLockValue,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
            ref: formBlockRef,
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.RecordProvider, {
                isNew: false,
                record: currentRecord,
                parent: null == parentRecord ? void 0 : parentRecord.data,
                children: props.children
            })
        })
    });
};
const DetailsBlockProvider = (0, __WEBPACK_EXTERNAL_MODULE__application_hoc_withDynamicSchemaProps_mjs__.withDynamicSchemaProps)((props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__BlockProvider_mjs__.BlockProvider, {
        name: "details",
        ...props,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalDetailsBlockProvider, {
            ...props
        })
    }));
const useDetailsBlockContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DetailsBlockContext);
const useDetailsBlockProps = ()=>{
    var _ctx_service_data, _ctx_service;
    const ctx = useDetailsBlockContext();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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 === _ctx_service ? void 0 : null === (_ctx_service_data = _ctx_service.data) || void 0 === _ctx_service_data ? void 0 : null === (_ctx_service_data_data = _ctx_service_data.data) || void 0 === _ctx_service_data_data ? void 0 : _ctx_service_data_data[0] : null === (_ctx_service1 = ctx.service) || void 0 === _ctx_service1 ? void 0 : null === (_ctx_service_data1 = _ctx_service1.data) || void 0 === _ctx_service_data1 ? 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 === _ctx_service ? void 0 : null === (_ctx_service_data = _ctx_service.data) || void 0 === _ctx_service_data ? void 0 : _ctx_service_data.data,
        ctx.service.loading
    ]);
    return {
        form: ctx.form
    };
};
export { DetailsBlockContext, DetailsBlockProvider, useDetailsBlockContext, useDetailsBlockProps };
