"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (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 = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (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__, {
    useDataBlockResource: ()=>useDataBlockResource,
    DataBlockResourceContext: ()=>DataBlockResourceContext,
    DataBlockResourceProvider: ()=>DataBlockResourceProvider
});
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) ? _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;
}
exports.DataBlockResourceContext = __webpack_exports__.DataBlockResourceContext;
exports.DataBlockResourceProvider = __webpack_exports__.DataBlockResourceProvider;
exports.useDataBlockResource = __webpack_exports__.useDataBlockResource;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "DataBlockResourceContext",
    "DataBlockResourceProvider",
    "useDataBlockResource"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
