import "choerodon-ui/lib/select/style";
import _Select from "choerodon-ui/lib/select";
import "choerodon-ui/lib/modal/style";
import _Modal from "choerodon-ui/lib/modal";

var _class;

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

/* eslint-disable no-underscore-dangle */
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import { injectIntl, FormattedMessage } from 'react-intl';
import { Content } from '@choerodon/boot';
import _ from 'lodash';
import uuidv1 from 'uuid/v1';
import Cookies from 'universal-cookie';
import { removeEndsChar } from "../../utils";
import Term from "../term";
import "../log-siderbar/index.less";
var Sidebar = _Modal.Sidebar;
var Option = _Select.Option;
var cookies = new Cookies();

var getAccessToken = function getAccessToken() {
  return cookies.get('access_token');
};

var TermSidebar = observer(_class = injectIntl(_class = /*#__PURE__*/function (_Component) {
  _inherits(TermSidebar, _Component);

  var _super = _createSuper(TermSidebar);

  function TermSidebar(props) {
    var _this;

    _classCallCheck(this, TermSidebar);

    _this = _super.call(this, props);

    _this.handleChange = function (value) {
      var _value$split = value.split('+'),
          _value$split2 = _slicedToArray(_value$split, 2),
          logId = _value$split2[0],
          containerName = _value$split2[1]; // eslint-disable-next-line react/destructuring-assignment


      if (logId !== _this.state.logId) {
        _this.setState({
          containerName: containerName,
          logId: logId
        });
      }
    };

    _this.state = {
      containerName: '',
      logId: null
    };
    return _this;
  }

  _createClass(TermSidebar, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var containers = this.props.record.containers;
      var _containers$ = containers[0],
          name = _containers$.name,
          logId = _containers$.logId;
      this.setState({
        containerName: name,
        logId: logId
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          visible = _this$props.visible,
          onClose = _this$props.onClose,
          record = _this$props.record,
          propsProjectId = _this$props.projectId,
          propsClusterId = _this$props.clusterId;
      var _this$state = this.state,
          logId = _this$state.logId,
          containerName = _this$state.containerName;

      var _ref = record || {},
          namespace = _ref.namespace,
          podName = _ref.name,
          containers = _ref.containers;

      var clusterId = propsClusterId || (record === null || record === void 0 ? void 0 : record.clusterId);
      var projectId = propsProjectId || (record === null || record === void 0 ? void 0 : record.projectId);
      var wsUrl = removeEndsChar(window._env_.DEVOPS_HOST, '/'); // const authToken = document.cookie.split('=')[1];

      var key = "cluster:".concat(clusterId, ".exec:").concat(uuidv1());
      var secretKey = window._env_.DEVOPS_WEBSOCKET_SECRET_KEY;
      var url = "".concat(wsUrl, "/websocket?key=").concat(key, "&group=from_front:").concat(key, "&processor=front_exec&secret_key=").concat(secretKey, "&env=").concat(namespace, "&podName=").concat(podName, "&containerName=").concat(containerName, "&logId=").concat(logId, "&clusterId=").concat(clusterId, "&oauthToken=").concat(getAccessToken(), "&projectId=").concat(projectId);

      var containerOptions = _.map(containers, function (container) {
        var id = container.logId,
            name = container.name;
        return /*#__PURE__*/React.createElement(Option, {
          key: logId,
          value: "".concat(id, "+").concat(name)
        }, name);
      });

      return /*#__PURE__*/React.createElement(Sidebar, {
        visible: visible,
        title: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "container.term"
        }),
        onOk: onClose,
        className: "c7n-container-sidebar c7n-region",
        okText: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "close"
        }),
        okCancel: false
      }, /*#__PURE__*/React.createElement(Content, {
        className: "sidebar-content"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7n-container-sidebar-content"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7n-term-title"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "container.term.ex"
      }), "\xA0", /*#__PURE__*/React.createElement(_Select, {
        className: "c7n-log-siderbar-select",
        value: containerName,
        onChange: this.handleChange
      }, containerOptions)), /*#__PURE__*/React.createElement("div", {
        className: "c7n-term-wrap"
      }, logId && /*#__PURE__*/React.createElement(Term, {
        url: url,
        id: logId
      })))));
    }
  }]);

  return TermSidebar;
}(Component)) || _class) || _class;

export { TermSidebar as default };