"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__, {
    ResourceActionContext: ()=>ResourceActionContext,
    useResourceActionContext: ()=>useResourceActionContext,
    useResourceContext: ()=>useResourceContext,
    ResourceActionProvider: ()=>ResourceActionProvider,
    useDataSourceFromRAC: ()=>useDataSourceFromRAC
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const schema_namespaceObject = require("@tachybase/schema");
const external_index_js_namespaceObject = require("./index.js");
const external_index_js_namespaceObject_1 = require("../index.js");
const index_js_namespaceObject = require("../api-client/index.js");
const ResourceActionContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
ResourceActionContext.displayName = 'ResourceActionContext';
const ResourceContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
ResourceContext.displayName = 'ResourceContext';
const CollectionResourceActionProvider = (props)=>{
    var _request_params, _collection_fields_filter, _collection_fields, _request_params1;
    const { collection, request, uid, dragSort } = props;
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const record = (0, external_index_js_namespaceObject_1.useRecord)();
    const actionName = null == request ? void 0 : request.action;
    const others = {};
    if ('get' === actionName) others['filterByTk'] = record[collection.targetKey || collection.filterTargetKey || 'id'];
    const appends = (null == request ? void 0 : null === (_request_params = request.params) || void 0 === _request_params ? void 0 : _request_params.appends) || [];
    const service = (0, index_js_namespaceObject.useRequest)({
        ...request,
        params: {
            ...others,
            ...null == request ? void 0 : request.params,
            appends: [
                ...(null == collection ? void 0 : null === (_collection_fields = collection.fields) || void 0 === _collection_fields ? void 0 : null === (_collection_fields_filter = _collection_fields.filter) || void 0 === _collection_fields_filter ? void 0 : _collection_fields_filter.call(_collection_fields, (field)=>field.target).map((field)=>field.name)) || [],
                ...appends
            ],
            sort: dragSort ? [
                true === collection.sortable ? 'sort' : collection.sortable
            ] : null == request ? void 0 : null === (_request_params1 = request.params) || void 0 === _request_params1 ? void 0 : _request_params1.sort
        }
    }, {
        uid
    });
    const resource = api.resource(request.resource);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ResourceContext.Provider, {
        value: {
            type: 'collection',
            resource,
            collection
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ResourceActionContext.Provider, {
            value: {
                ...service,
                defaultRequest: request,
                dragSort
            },
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject_1.CollectionProvider_deprecated, {
                collection: collection,
                children: props.children
            })
        })
    });
};
const AssociationResourceActionProvider = (props)=>{
    var _request_params, _collection_fields_filter, _collection_fields;
    const { collection, association, request, uid, dragSort } = props;
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const record = (0, external_index_js_namespaceObject_1.useRecord)();
    const resourceOf = record[association.sourceKey];
    const appends = (null == request ? void 0 : null === (_request_params = request.params) || void 0 === _request_params ? void 0 : _request_params.appends) || [];
    const service = (0, index_js_namespaceObject.useRequest)({
        resourceOf,
        ...request,
        params: {
            ...null == request ? void 0 : request.params,
            appends: [
                ...(null == collection ? void 0 : null === (_collection_fields = collection.fields) || void 0 === _collection_fields ? void 0 : null === (_collection_fields_filter = _collection_fields.filter) || void 0 === _collection_fields_filter ? void 0 : _collection_fields_filter.call(_collection_fields, (field)=>field.target).map((field)=>field.name)) || [],
                ...appends
            ]
        }
    }, {
        uid
    });
    const resource = api.resource(request.resource, resourceOf);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ResourceContext.Provider, {
        value: {
            type: 'association',
            resource,
            association,
            collection
        },
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ResourceActionContext.Provider, {
            value: {
                ...service,
                defaultRequest: request,
                dragSort
            },
            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_index_js_namespaceObject_1.CollectionProvider_deprecated, {
                collection: collection,
                children: props.children
            })
        })
    });
};
const ResourceActionProvider = (props)=>{
    var _request_resource;
    let { collection, request } = props;
    const { getCollection } = (0, external_index_js_namespaceObject.useCollectionManager_deprecated)();
    if ('string' == typeof collection) collection = getCollection(collection);
    if (!collection) return null;
    if (null == request ? void 0 : null === (_request_resource = request.resource) || void 0 === _request_resource ? void 0 : _request_resource.includes('.')) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(AssociationResourceActionProvider, {
        ...props,
        collection: collection
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(CollectionResourceActionProvider, {
        ...props,
        collection: collection
    });
};
const useResourceActionContext = ()=>(0, external_react_namespaceObject.useContext)(ResourceActionContext);
const useDataSourceFromRAC = (options)=>{
    const service = (0, external_react_namespaceObject.useContext)(ResourceActionContext);
    const field = (0, schema_namespaceObject.useField)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!service.loading) {
            null == options || options.onSuccess(service.data);
            field.componentProps.dragSort = !!service.dragSort;
        }
    }, [
        service.loading
    ]);
    return service;
};
const useResourceContext = ()=>{
    const { type, resource, collection, association } = (0, external_react_namespaceObject.useContext)(ResourceContext);
    return {
        type,
        resource,
        collection,
        association,
        targetKey: (null == association ? void 0 : association.targetKey) || (null == collection ? void 0 : collection.targetKey) || 'id'
    };
};
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
});
