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_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/index.mjs";
const CollectionHistoryContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)({
    historyCollections: [],
    refreshCH: ()=>void 0
});
CollectionHistoryContext.displayName = 'CollectionHistoryContext';
const CollectionHistoryProvider = (props)=>{
    var _service_data;
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const options = {
        resource: 'collectionsHistory',
        action: 'list',
        params: {
            paginate: false,
            appends: [
                'fields'
            ],
            filter: {},
            sort: [
                'sort'
            ]
        }
    };
    const location = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useLocation)();
    const service = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(options, {
        manual: true
    });
    const isAdminPage = location.pathname.startsWith('/admin');
    const token = api.auth.getToken() || '';
    (0, __WEBPACK_EXTERNAL_MODULE_react__.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__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CollectionHistoryContext.Provider, {
        value: {
            historyCollections: null === (_service_data = service.data) || void 0 === _service_data ? void 0 : _service_data.data,
            refreshCH
        },
        children: props.children
    });
};
const useHistoryCollectionsByNames = (collectionNames)=>{
    const { historyCollections } = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CollectionHistoryContext);
    return historyCollections.filter((i)=>collectionNames.includes(i.name));
};
const useCollectionHistory = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CollectionHistoryContext);
export { CollectionHistoryProvider, useCollectionHistory, useHistoryCollectionsByNames };
