import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect } from "react";
import { useLocation } from "react-router-dom";
import { useAPIClient, useRequest } from "../api-client/index.mjs";
const CollectionHistoryContext = /*#__PURE__*/ createContext({
    historyCollections: [],
    refreshCH: ()=>void 0
});
CollectionHistoryContext.displayName = 'CollectionHistoryContext';
const CollectionHistoryProvider = (props)=>{
    var _service_data;
    const api = useAPIClient();
    const options = {
        resource: 'collectionsHistory',
        action: 'list',
        params: {
            paginate: false,
            appends: [
                'fields'
            ],
            filter: {},
            sort: [
                'sort'
            ]
        }
    };
    const location = useLocation();
    const service = useRequest(options, {
        manual: true
    });
    const isAdminPage = location.pathname.startsWith('/admin');
    const token = api.auth.getToken() || '';
    useEffect(()=>{
        if (isAdminPage && token) service.run();
    }, [
        isAdminPage,
        token
    ]);
    const refreshCH = async ()=>{
        const { data } = await api.request(options);
        service.mutate(data);
        return (null == data ? void 0 : data.data) || [];
    };
    if (service.loading) return;
    return /*#__PURE__*/ jsx(CollectionHistoryContext.Provider, {
        value: {
            historyCollections: null == (_service_data = service.data) ? void 0 : _service_data.data,
            refreshCH
        },
        children: props.children
    });
};
const useHistoryCollectionsByNames = (collectionNames)=>{
    const { historyCollections } = useContext(CollectionHistoryContext);
    return historyCollections.filter((i)=>collectionNames.includes(i.name));
};
const useCollectionHistory = ()=>useContext(CollectionHistoryContext);
export { CollectionHistoryProvider, useCollectionHistory, useHistoryCollectionsByNames };
