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__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "./index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/index.mjs";
const ResourceActionContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
ResourceActionContext.displayName = 'ResourceActionContext';
const ResourceContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ResourceContext.Provider, {
        value: {
            type: 'collection',
            resource,
            collection
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ResourceActionContext.Provider, {
            value: {
                ...service,
                defaultRequest: request,
                dragSort
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.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, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ResourceContext.Provider, {
        value: {
            type: 'association',
            resource,
            association,
            collection
        },
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(ResourceActionContext.Provider, {
            value: {
                ...service,
                defaultRequest: request,
                dragSort
            },
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.CollectionProvider_deprecated, {
                collection: collection,
                children: props.children
            })
        })
    });
};
const ResourceActionProvider = (props)=>{
    var _request_resource;
    let { collection, request } = props;
    const { getCollection } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(AssociationResourceActionProvider, {
        ...props,
        collection: collection
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionResourceActionProvider, {
        ...props,
        collection: collection
    });
};
const useResourceActionContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(ResourceActionContext);
const useDataSourceFromRAC = (options)=>{
    const service = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(ResourceActionContext);
    const field = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useField)();
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(ResourceContext);
    return {
        type,
        resource,
        collection,
        association,
        targetKey: (null == association ? void 0 : association.targetKey) || (null == collection ? void 0 : collection.targetKey) || 'id'
    };
};
export { ResourceActionContext, ResourceActionProvider, useDataSourceFromRAC, useResourceActionContext, useResourceContext };
