"use strict";
var __webpack_require__ = {};
(()=>{
    __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__, {
    DataBlockResourceProvider: ()=>DataBlockResourceProvider,
    useDataBlockResource: ()=>useDataBlockResource,
    DataBlockResourceContext: ()=>DataBlockResourceContext
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const index_js_namespaceObject = require("../../api-client/index.js");
const external_collection_index_js_namespaceObject = require("../collection/index.js");
const external_collection_record_index_js_namespaceObject = require("../collection-record/index.js");
const external_utils_js_namespaceObject = require("../utils.js");
const external_DataBlockProvider_js_namespaceObject = require("./DataBlockProvider.js");
const DataBlockResourceContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
DataBlockResourceContext.displayName = 'DataBlockResourceContext';
const DataBlockResourceProvider = (param)=>{
    let { children } = param;
    const dataBlockProps = (0, external_DataBlockProvider_js_namespaceObject.useDataBlockProps)();
    const cm = (0, external_collection_index_js_namespaceObject.useCollectionManager)();
    const { association, collection, dataSource, sourceId, parentRecord } = dataBlockProps;
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const headers = (0, external_utils_js_namespaceObject.useDataSourceHeaders)(dataSource);
    const collectionName = (0, external_react_namespaceObject.useMemo)(()=>'string' == typeof collection ? collection : null == collection ? void 0 : collection.name, [
        collection
    ]);
    const sourceIdValue = (0, external_react_namespaceObject.useMemo)(()=>{
        if (sourceId) return sourceId;
        if (association && parentRecord) {
            const sourceKey = cm.getSourceKeyByAssocation(association);
            const parentRecordData = parentRecord instanceof external_collection_record_index_js_namespaceObject.CollectionRecord ? parentRecord.data : parentRecord;
            return parentRecordData[sourceKey];
        }
    }, [
        association,
        sourceId,
        parentRecord
    ]);
    const resource = (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(DataBlockResourceContext.Provider, {
        value: resource,
        children: children
    });
};
function useDataBlockResource() {
    const context = (0, external_react_namespaceObject.useContext)(DataBlockResourceContext);
    if (!context) throw new Error('useDataBlockResource() must be used within a DataBlockResourceProvider');
    return context;
}
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
});
