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__ant_design_icons__ from "@ant-design/icons";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__components_CollectionDeletedPlaceholder_mjs__ from "../components/CollectionDeletedPlaceholder.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__DataSourceManagerProvider_mjs__ from "./DataSourceManagerProvider.mjs";
const DataSourceContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
DataSourceContext.displayName = 'DataSourceContext';
const DataSourceProvider = (param)=>{
    let { children, dataSource } = param;
    const dataSourceManager = (0, __WEBPACK_EXTERNAL_MODULE__DataSourceManagerProvider_mjs__.useDataSourceManager)();
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { refresh } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useSchemaComponentContext)();
    const [_, setRandom] = __WEBPACK_EXTERNAL_MODULE_react__["default"].useState(0);
    const dataSourceValue = dataSourceManager.getDataSource(dataSource);
    if (!dataSourceValue) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__components_CollectionDeletedPlaceholder_mjs__.CollectionDeletedPlaceholder, {
        type: "DataSource",
        name: dataSource
    });
    if ('loading-failed' === dataSourceValue.status) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__components_CollectionDeletedPlaceholder_mjs__.CollectionDeletedPlaceholder, {
        type: "DataSource",
        name: dataSourceValue.displayName || dataSource,
        message: dataSourceValue.errorMessage || 'loading failed'
    });
    if ('loading' === dataSourceValue.status || 'reloading' === dataSourceValue.status) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.CardItem, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Result, {
            icon: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__ant_design_icons__.LoadingOutlined, {}),
            title: "".concat(dataSourceValue.displayName || dataSource, " ").concat(t('data source'), " ").concat(t('loading'), "..."),
            extra: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Button, {
                type: "primary",
                onClick: ()=>dataSourceValue.reload().then(()=>{
                        refresh();
                        setRandom(Math.random());
                    }),
                children: t('Refresh')
            })
        })
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(DataSourceContext.Provider, {
        value: dataSourceValue,
        children: children
    });
};
function useDataSource() {
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return context;
}
function useDataSourceKey() {
    const context = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(DataSourceContext);
    return null == context ? void 0 : context.key;
}
export { DataSourceContext, DataSourceProvider, useDataSource, useDataSourceKey };
