function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { useContext, useMemo, useEffect, createContext } from 'react';
import { inject } from 'mobx-react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { axios } from '@choerodon/boot';
import { DataSet } from 'choerodon-ui/pro/lib';
import { useClusterStore } from "../../../../stores";
import NodePodsDataSet from "./NodePodsDataSet";
import NodeInfoDataSet from "./NodeInfoDataSet";
import useStore from "./useStore";
var NodeContentStore = /*#__PURE__*/createContext();

var useNodeContentStore = function useNodeContentStore() {
  return useContext(NodeContentStore);
};

var NodeContentStoreProvider = injectIntl(inject('AppState')(observer(function (props) {
  var formatMessage = props.intl.formatMessage,
      projectId = props.AppState.currentMenuType.id,
      children = props.children;

  var _useClusterStore = useClusterStore(),
      clusterStore = _useClusterStore.clusterStore;

  var _clusterStore$getSele = clusterStore.getSelectedMenu,
      parentId = _clusterStore$getSele.parentId,
      name = _clusterStore$getSele.name;
  var tabs = useMemo(function () {
    return {
      RESOURCE_TAB: 'resource',
      MONITOR_TAB: 'monitor'
    };
  }, []);
  var nodePodsDs = useMemo(function () {
    return new DataSet(NodePodsDataSet());
  }, []);
  var nodeInfoDs = useMemo(function () {
    return new DataSet(NodeInfoDataSet());
  }, []);
  var contentStore = useStore(tabs);
  useEffect(function () {
    clusterStore.setNoHeader(true);
  }, []);
  var tabkey = contentStore.getTabKey;
  useEffect(function () {
    if (tabkey === tabs.RESOURCE_TAB) {
      nodeInfoDs.transport.read.url = "devops/v1/projects/".concat(projectId, "/clusters/nodes?cluster_id=").concat(parentId, "&node_name=").concat(name);
      nodeInfoDs.query();
      nodePodsDs.transport.read.url = "devops/v1/projects/".concat(projectId, "/clusters/page_node_pods?cluster_id=").concat(parentId, "&node_name=").concat(name);
      nodePodsDs.query();
    } else {
      loadMonitor();
    }
  }, [projectId, name, parentId, tabkey]);

  function loadMonitor() {
    return _loadMonitor.apply(this, arguments);
  }

  function _loadMonitor() {
    _loadMonitor = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var res, uri;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return contentStore.loadGrafanaUrl(projectId, parentId);

            case 2:
              res = _context.sent;

              if (res) {
                uri = escape("".concat(window.location));
                axios.get('/oauth/is_login', {
                  withCredentials: true
                }).then(function (response) {
                  if (!response) {
                    window.location = "".concat(window._env_.API_HOST, "/oauth/oauth/authorize?response_type=token&client_id=").concat(window._env_.CLIENT_ID, "&state=&redirect_uri=").concat(uri, "%26redirectFlag");
                  }
                });
              }

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));
    return _loadMonitor.apply(this, arguments);
  }

  var value = {
    formatMessage: formatMessage,
    projectId: projectId,
    nodePodsDs: nodePodsDs,
    nodeInfoDs: nodeInfoDs,
    tabs: tabs,
    contentStore: contentStore,
    COMPONENT_TAB: 'component'
  };
  return /*#__PURE__*/React.createElement(NodeContentStore.Provider, {
    value: value
  }, children);
})));
export { useNodeContentStore, NodeContentStoreProvider };