import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect } from "react";
import { useField } from "@tachybase/schema";
import { useCollectionManager_deprecated } from "./index.mjs";
import { CollectionProvider_deprecated, useRecord } from "../index.mjs";
import { useAPIClient, useRequest } from "../api-client/index.mjs";
const ResourceActionContext = /*#__PURE__*/ createContext(null);
ResourceActionContext.displayName = 'ResourceActionContext';
const ResourceContext = /*#__PURE__*/ 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 = useAPIClient();
    const record = 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.appends) || [];
    const service = useRequest({
        ...request,
        params: {
            ...others,
            ...null == request ? void 0 : request.params,
            appends: [
                ...(null == collection ? void 0 : null == (_collection_fields = collection.fields) ? void 0 : null == (_collection_fields_filter = _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.sort
        }
    }, {
        uid
    });
    const resource = api.resource(request.resource);
    return /*#__PURE__*/ jsx(ResourceContext.Provider, {
        value: {
            type: 'collection',
            resource,
            collection
        },
        children: /*#__PURE__*/ jsx(ResourceActionContext.Provider, {
            value: {
                ...service,
                defaultRequest: request,
                dragSort
            },
            children: /*#__PURE__*/ jsx(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 = useAPIClient();
    const record = useRecord();
    const resourceOf = record[association.sourceKey];
    const appends = (null == request ? void 0 : null == (_request_params = request.params) ? void 0 : _request_params.appends) || [];
    const service = useRequest({
        resourceOf,
        ...request,
        params: {
            ...null == request ? void 0 : request.params,
            appends: [
                ...(null == collection ? void 0 : null == (_collection_fields = collection.fields) ? void 0 : null == (_collection_fields_filter = _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__*/ jsx(ResourceContext.Provider, {
        value: {
            type: 'association',
            resource,
            association,
            collection
        },
        children: /*#__PURE__*/ jsx(ResourceActionContext.Provider, {
            value: {
                ...service,
                defaultRequest: request,
                dragSort
            },
            children: /*#__PURE__*/ jsx(CollectionProvider_deprecated, {
                collection: collection,
                children: props.children
            })
        })
    });
};
const ResourceActionProvider = (props)=>{
    var _request_resource;
    let { collection, request } = props;
    const { getCollection } = 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.includes('.')) return /*#__PURE__*/ jsx(AssociationResourceActionProvider, {
        ...props,
        collection: collection
    });
    return /*#__PURE__*/ jsx(CollectionResourceActionProvider, {
        ...props,
        collection: collection
    });
};
const useResourceActionContext = ()=>useContext(ResourceActionContext);
const useDataSourceFromRAC = (options)=>{
    const service = useContext(ResourceActionContext);
    const field = useField();
    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 } = 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 };
