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__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_index_mjs__ from "../collection/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__collection_record_index_mjs__ from "../collection-record/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_mjs__ from "../utils.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__DataBlockProvider_mjs__ from "./DataBlockProvider.mjs";
const DataBlockResourceContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
DataBlockResourceContext.displayName = 'DataBlockResourceContext';
const DataBlockResourceProvider = (param)=>{
    let { children } = param;
    const dataBlockProps = (0, __WEBPACK_EXTERNAL_MODULE__DataBlockProvider_mjs__.useDataBlockProps)();
    const cm = (0, __WEBPACK_EXTERNAL_MODULE__collection_index_mjs__.useCollectionManager)();
    const { association, collection, dataSource, sourceId, parentRecord } = dataBlockProps;
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const headers = (0, __WEBPACK_EXTERNAL_MODULE__utils_mjs__.useDataSourceHeaders)(dataSource);
    const collectionName = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>'string' == typeof collection ? collection : null == collection ? void 0 : collection.name, [
        collection
    ]);
    const sourceIdValue = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (sourceId) return sourceId;
        if (association && parentRecord) {
            const sourceKey = cm.getSourceKeyByAssocation(association);
            const parentRecordData = parentRecord instanceof __WEBPACK_EXTERNAL_MODULE__collection_record_index_mjs__.CollectionRecord ? parentRecord.data : parentRecord;
            return parentRecordData[sourceKey];
        }
    }, [
        association,
        sourceId,
        parentRecord
    ]);
    const resource = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (association) return api.resource(association, sourceIdValue, headers);
        var _dataBlockProps_resource_deprecated;
        return api.resource(null !== (_dataBlockProps_resource_deprecated = dataBlockProps.resource_deprecated) && void 0 !== _dataBlockProps_resource_deprecated ? _dataBlockProps_resource_deprecated : collectionName, void 0, headers);
    }, [
        api,
        association,
        collection,
        sourceIdValue,
        headers,
        dataBlockProps.resource_deprecated
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DataBlockResourceContext.Provider, {
        value: resource,
        children: children
    });
};
function useDataBlockResource() {
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataBlockResourceContext);
    if (!context) throw new Error('useDataBlockResource() must be used within a DataBlockResourceProvider');
    return context;
}
export { DataBlockResourceContext, DataBlockResourceProvider, useDataBlockResource };
