"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useDetailsBlockProps: ()=>useDetailsBlockProps,
    DetailsBlockContext: ()=>DetailsBlockContext,
    DetailsBlockProvider: ()=>DetailsBlockProvider,
    useDetailsBlockContext: ()=>useDetailsBlockContext
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_antd_namespaceObject = require("antd");
const external_lodash_namespaceObject = require("lodash");
var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
const withDynamicSchemaProps_js_namespaceObject = require("../application/hoc/withDynamicSchemaProps.js");
const CollectionRecordProvider_js_namespaceObject = require("../data-source/collection-record/CollectionRecordProvider.js");
const index_js_namespaceObject = require("../record-provider/index.js");
const external_BlockProvider_js_namespaceObject = require("./BlockProvider.js");
const external_hooks_index_js_namespaceObject = require("./hooks/index.js");
const DetailsBlockContext = /*#__PURE__*/ (0, external_react_namespaceObject.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, schema_namespaceObject.useField)();
    const form = (0, external_react_namespaceObject.useMemo)(()=>(0, schema_namespaceObject.createForm)({
            readPretty
        }), [
        readPretty
    ]);
    const { resource, service } = (0, external_BlockProvider_js_namespaceObject.useBlockRequestContext)();
    const parentRecord = (0, CollectionRecordProvider_js_namespaceObject.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, external_react_namespaceObject.useRef)();
    const detailsBLockValue = (0, external_react_namespaceObject.useMemo)(()=>({
            action,
            form,
            field,
            service,
            resource,
            formBlockRef
        }), [
        action,
        field,
        form,
        resource,
        service
    ]);
    const { filter } = (0, external_hooks_index_js_namespaceObject.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, external_react_namespaceObject.useEffect)(()=>{
        if (!external_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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {});
    field.loaded = true;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(DetailsBlockContext.Provider, {
        value: detailsBLockValue,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            ref: formBlockRef,
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_js_namespaceObject.RecordProvider, {
                isNew: false,
                record: currentRecord,
                parent: null == parentRecord ? void 0 : parentRecord.data,
                children: props.children
            })
        })
    });
};
const DetailsBlockProvider = (0, withDynamicSchemaProps_js_namespaceObject.withDynamicSchemaProps)((props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_BlockProvider_js_namespaceObject.BlockProvider, {
        name: "details",
        ...props,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InternalDetailsBlockProvider, {
            ...props
        })
    }));
const useDetailsBlockContext = ()=>(0, external_react_namespaceObject.useContext)(DetailsBlockContext);
const useDetailsBlockProps = ()=>{
    var _ctx_service_data, _ctx_service;
    const ctx = useDetailsBlockContext();
    (0, external_react_namespaceObject.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
    };
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
