import PropTypes from 'prop-types';
import { useMemoizedFn } from 'ahooks';
import { createContext, useState, useContext, useMemo } from 'react';
import { jsx as _jsx } from "react/jsx-runtime";
const AppInfoContext = /*#__PURE__*/createContext({});

/**
 * @typedef {Object} AppInfo
 * @property {string} icon
 * @property {string} name
 * @property {string} description
 * @property {Array} badges
 * @property {Array} actions
 * @property {Array} tabs
 * @property {string} currentTab
 */
/**
 * @param {{ app?: AppInfo, children?: import('react').ReactNode }} props
 */
export function AppInfoProvider({
  currentTab = '',
  children = null
}) {
  const [appInfo, setAppInfo] = useState({});
  const updateAppInfo = useMemoizedFn(patch => {
    setAppInfo(prev => ({
      ...prev,
      ...patch
    }));
  });
  const value = useMemo(() => {
    const {
      tabs = []
    } = appInfo;
    const currentTabConfig = tabs.find(v => v.value === currentTab);
    let _ctab = currentTab;
    let TabComponent = null;
    // fallback 到第一个 tab
    if (!currentTabConfig && tabs.length > 0) {
      _ctab = tabs[0].value;
      TabComponent = tabs[0].render || null;
    } else {
      TabComponent = currentTabConfig?.render || null;
    }
    return {
      ...appInfo,
      currentTab: _ctab,
      TabComponent,
      updateAppInfo
    };
  },
  // eslint-disable-next-line react-hooks/exhaustive-deps
  [appInfo, currentTab, updateAppInfo]);
  return /*#__PURE__*/_jsx(AppInfoContext.Provider, {
    value: value,
    children: children
  });
}
AppInfoProvider.propTypes = {
  currentTab: PropTypes.string,
  children: PropTypes.node
};
export function useAppInfo() {
  const context = useContext(AppInfoContext) || {};
  return context;
}