"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__, {
    CollectionHistoryProvider: ()=>CollectionHistoryProvider,
    useCollectionHistory: ()=>useCollectionHistory,
    useHistoryCollectionsByNames: ()=>useHistoryCollectionsByNames
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_react_router_dom_namespaceObject = require("react-router-dom");
const index_js_namespaceObject = require("../api-client/index.js");
const CollectionHistoryContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({
    historyCollections: [],
    refreshCH: ()=>void 0
});
CollectionHistoryContext.displayName = 'CollectionHistoryContext';
const CollectionHistoryProvider = (props)=>{
    var _service_data;
    const api = (0, index_js_namespaceObject.useAPIClient)();
    const options = {
        resource: 'collectionsHistory',
        action: 'list',
        params: {
            paginate: false,
            appends: [
                'fields'
            ],
            filter: {},
            sort: [
                'sort'
            ]
        }
    };
    const location = (0, external_react_router_dom_namespaceObject.useLocation)();
    const service = (0, index_js_namespaceObject.useRequest)(options, {
        manual: true
    });
    const isAdminPage = location.pathname.startsWith('/admin');
    const token = api.auth.getToken() || '';
    (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.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, external_react_namespaceObject.useContext)(CollectionHistoryContext);
    return historyCollections.filter((i)=>collectionNames.includes(i.name));
};
const useCollectionHistory = ()=>(0, external_react_namespaceObject.useContext)(CollectionHistoryContext);
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
});
