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__CollectionRecord_mjs__ from "./CollectionRecord.mjs";
const CollectionRecordContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
CollectionRecordContext.displayName = 'CollectionRecordContext';
const CollectionRecordProvider = (param)=>{
    let { isNew, record, parentRecord, children } = param;
    const parentRecordValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (parentRecord) {
            if (parentRecord instanceof __WEBPACK_EXTERNAL_MODULE__CollectionRecord_mjs__.CollectionRecord) return parentRecord;
            return new __WEBPACK_EXTERNAL_MODULE__CollectionRecord_mjs__.CollectionRecord({
                data: parentRecord
            });
        }
        if (record instanceof __WEBPACK_EXTERNAL_MODULE__CollectionRecord_mjs__.CollectionRecord) return record.parentRecord;
    }, [
        parentRecord,
        record
    ]);
    const currentRecordValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        let res;
        if (record) {
            if (record instanceof __WEBPACK_EXTERNAL_MODULE__CollectionRecord_mjs__.CollectionRecord) {
                res = record;
                res.isNew = record.isNew || isNew;
            } else res = new __WEBPACK_EXTERNAL_MODULE__CollectionRecord_mjs__.CollectionRecord({
                data: record,
                isNew
            });
        } else res = new __WEBPACK_EXTERNAL_MODULE__CollectionRecord_mjs__.CollectionRecord({
            isNew
        });
        res.setParentRecord(parentRecordValue);
        return res;
    }, [
        record,
        parentRecordValue,
        isNew
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionRecordContext.Provider, {
        value: currentRecordValue,
        children: children
    });
};
function useCollectionRecord() {
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CollectionRecordContext);
    return context;
}
function useCollectionRecordData() {
    const record = useCollectionRecord();
    return null == record ? void 0 : record.data;
}
function useCollectionParentRecord() {
    const record = useCollectionRecord();
    return null == record ? void 0 : record.parentRecord;
}
function useCollectionParentRecordData() {
    var _record_parentRecord;
    const record = useCollectionRecord();
    return null == record ? void 0 : null === (_record_parentRecord = record.parentRecord) || void 0 === _record_parentRecord ? void 0 : _record_parentRecord.data;
}
export { CollectionRecordContext, CollectionRecordProvider, useCollectionParentRecord, useCollectionParentRecordData, useCollectionRecord, useCollectionRecordData };
