import "choerodon-ui/pro/lib/data-set/style";
import _DataSet from "choerodon-ui/pro/lib/data-set";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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); }); }; }

/* eslint-disable */
import React, { createContext, useMemo, useContext, useEffect } from 'react';
import { inject } from 'mobx-react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { axios } from '@choerodon/boot';
import { useClusterStore } from "../../../../stores";
import { useClusterMainStore } from "../../../stores";
import useStore from "./useStore";
import NodeListDataSet from "./NodeListDataSet";
import PermissionDataSet from "./PermissionDataSet";
import PolarisNumDataSet from "./PalarisNumDataSet";
import getTablePostData from "../../../../../../utils/getTablePostData";
import SummaryDataSet from "./SummaryDataSet";
import EnvDetailDataSet from "./EnvDetailDataSet";
import EnvManageListDs from "./EnvManageListDataSet";
import EnvQuotaFormDataSet from "./EnvQuotaFormDataSet";
var Store = /*#__PURE__*/createContext();
export function useClusterContentStore() {
  return useContext(Store);
}
export var StoreProvider = injectIntl(inject('AppState')(observer(function (props) {
  var tabs = useMemo(function () {
    return {
      NODE_TAB: 'node',
      ENV_TAB: 'env',
      POLARIS_TAB: 'polaris',
      ASSIGN_TAB: 'assign',
      COMPONENT_TAB: 'component',
      MONITOR_TAB: 'monitor'
    };
  }, []);
  var formatMessage = props.intl.formatMessage,
      projectId = props.AppState.currentMenuType.id,
      children = props.children;

  var _useClusterMainStore = useClusterMainStore(),
      ClusterDetailDs = _useClusterMainStore.ClusterDetailDs,
      mainStore = _useClusterMainStore.mainStore;

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

  var id = clusterStore.getSelectedMenu.id;
  var intlPrefix = 'c7ncd.cluster';
  useEffect(function () {
    clusterStore.setNoHeader(false);
  }, []);
  useEffect(function () {
    if (mainStore.getClusterDefaultTab) {
      contentStore.setTabKey(mainStore.getClusterDefaultTab);
      mainStore.setClusterDefaultTab(null);
    }
  }, [mainStore.getClusterDefaultTab]);
  var record = ClusterDetailDs.current;
  var NodeListDs = useMemo(function () {
    return new _DataSet(NodeListDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix
    }));
  }, []);
  var EnvMgListDs = useMemo(function () {
    return new _DataSet(EnvManageListDs({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix
    }));
  }, []);
  var EnvQuotaFormDs = useMemo(function () {
    return new _DataSet(EnvQuotaFormDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix
    }));
  }, []);
  var PermissionDs = useMemo(function () {
    return new _DataSet(PermissionDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix,
      projectId: projectId,
      id: id,
      skipCheckProjectPermission: record && record.get('skipCheckProjectPermission')
    }));
  }, [record]);
  var clusterSummaryDs = useMemo(function () {
    return new _DataSet(SummaryDataSet());
  }, []);
  var envDetailDs = useMemo(function () {
    return new _DataSet(EnvDetailDataSet());
  }, []);
  var polarisNumDS = useMemo(function () {
    return new _DataSet(PolarisNumDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix,
      projectId: projectId,
      id: id
    }));
  }, [record]);
  var contentStore = useStore(tabs);
  var URL = '';
  var tabkey = contentStore.getTabKey;
  useEffect(function () {
    switch (tabkey) {
      case tabs.NODE_TAB:
        NodeListDs.transport.read.url = "/devops/v1/projects/".concat(projectId, "/clusters/page_nodes?cluster_id=").concat(id);
        NodeListDs.query();
        break;

      case tabs.ENV_TAB:
        EnvMgListDs.loadData([{
          envDetail: 'hellowrold',
          projects: 'devops',
          createTime: '',
          cpu: 'sadsadas',
          stack: 'sadas'
        }]);
        break;

      case tabs.ASSIGN_TAB:
        if (!record) {
          return;
        }

        if (record.get('skipCheckProjectPermission')) {
          URL = "/devops/v1/projects/".concat(projectId, "/page_projects");
        } else {
          URL = "/devops/v1/projects/".concat(projectId, "/clusters/").concat(id, "/permission/page_related");
        }

        PermissionDs.transport.read = function (_ref) {
          var data = _ref.data;
          var postData = getTablePostData(data);
          return {
            url: URL,
            method: 'post',
            data: postData
          };
        };

        PermissionDs.transport.destroy = {
          url: "/devops/v1/projects/".concat(projectId, "/clusters/").concat(id, "/permission"),
          method: 'delete'
        };
        PermissionDs.query();
        break;

      case tabs.COMPONENT_TAB:
        contentStore.loadComponentList(projectId, id);
        break;

      case tabs.MONITOR_TAB:
        loadMonitor();
        break;

      case tabs.POLARIS_TAB:
        clusterSummaryDs.transport.read.url = "/devops/v1/projects/".concat(projectId, "/polaris/clusters/").concat(id, "/summary");
        envDetailDs.transport.read.url = "/devops/v1/projects/".concat(projectId, "/polaris/clusters/").concat(id, "/env_detail");
        polarisNumDS.transport.read.url = "devops/v1/projects/".concat(projectId, "/polaris/records?scope=cluster&scope_id=").concat(id);
        loadPolaris();
        break;

      default:
    }
  }, [projectId, tabkey, record]);

  function loadPolaris() {
    return _loadPolaris.apply(this, arguments);
  }

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

            case 2:
              res = _context.sent;

              if (res) {
                polarisNumDS.query();
                clusterSummaryDs.query();
                envDetailDs.query();
              }

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

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

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

            case 2:
              res = _context2.sent;

              if (res) {
                uri = escape("".concat(window.location));
                axios.get('oauth/public/is-online', {
                  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 _context2.stop();
          }
        }
      }, _callee2);
    }));
    return _loadMonitor.apply(this, arguments);
  }

  var value = _objectSpread(_objectSpread({}, props), {}, {
    tabs: tabs,
    contentStore: contentStore,
    NodeListDs: NodeListDs,
    PermissionDs: PermissionDs,
    intlPrefix: intlPrefix,
    formatMessage: formatMessage,
    ClusterDetailDs: ClusterDetailDs,
    polarisNumDS: polarisNumDS,
    projectId: projectId,
    clusterId: id,
    clusterSummaryDs: clusterSummaryDs,
    envDetailDs: envDetailDs,
    clusterStore: clusterStore,
    EnvMgListDs: EnvMgListDs,
    EnvQuotaFormDs: EnvQuotaFormDs
  });

  return /*#__PURE__*/React.createElement(Store.Provider, {
    value: value
  }, children);
})));