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