import "choerodon-ui/lib/modal/style";
import _Modal from "choerodon-ui/lib/modal";
import "choerodon-ui/lib/icon/style";
import _Icon from "choerodon-ui/lib/icon";
import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";
import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/lib/select/style";
import _Select from "choerodon-ui/lib/select";

var _dec, _class, _class2, _temp;

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

import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';
import { injectIntl, FormattedMessage } from 'react-intl';
import _ from 'lodash';
import PipelineCreateStore from "../../stores/PipelineCreateStore";
import TaskCreate from "../taskCreate";
import StageTitle from "../stageTitle";
import StageCreateModal from "../stageCreateModal";
import Tips from "../../../../components/Tips/Tips";
import ClickText from "../../../../components/click-text";
import { TASK_SERIAL, TASK_PARALLEL, TASK_TYPE_MANUAL, TRIGGER_TYPE_AUTO } from "../Constants";
import "./StageCard.less";
var Option = _Select.Option;
var StageCard = (_dec = inject('AppState'), injectIntl(_class = _dec(_class = observer(_class = (_temp = _class2 = /*#__PURE__*/function (_Component) {
  _inherits(StageCard, _Component);

  var _super = _createSuper(StageCard);

  function StageCard() {
    var _this;

    _classCallCheck(this, StageCard);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      taskName: '',
      taskId: null,
      stage: {},
      showTask: false,
      showTaskDelete: false,
      showStageDelete: false,
      showHeadModal: false,
      isRemoveHead: false,
      isEditHead: false
    };

    _this.handleSelect = function (value) {
      var stageId = _this.props.stageId;
      PipelineCreateStore.setTaskSettings(stageId, value);
    };

    _this.openTaskSidebar = function (e, id, isHead) {
      _this.setState({
        showTask: true,
        taskId: id,
        isEditHead: isHead
      });
    };

    _this.onCloseSidebar = function () {
      _this.setState({
        showTask: false,
        taskId: null,
        isEditHead: false
      });
    };

    _this.handleTaskDelete = function () {
      var stageId = _this.props.stageId;
      var _this$state = _this.state,
          taskId = _this$state.taskId,
          isRemoveHead = _this$state.isRemoveHead;
      PipelineCreateStore.removeTask(stageId, taskId, isRemoveHead);
      PipelineCreateStore.checkCanSubmit();

      _this.closeTaskRemove();
    };

    _this.closeTaskRemove = function () {
      _this.setState({
        showTaskDelete: false,
        taskId: null,
        taskName: '',
        isRemoveHead: false
      });
    };

    _this.handleHeadChange = function () {
      var stageId = _this.props.stageId;
      var stageList = PipelineCreateStore.getStageList;

      var stage = _.find(stageList, ['tempId', stageId]);

      _this.setState({
        showHeadModal: true,
        stage: stage
      });
    };

    _this.handleStageRemove = function () {
      var stageId = _this.props.stageId;
      PipelineCreateStore.removeStage(stageId);
      PipelineCreateStore.checkCanSubmit();

      _this.closeStageRemove();
    };

    _this.openStageRemove = function () {
      _this.setState({
        showStageDelete: true
      });
    };

    _this.closeStageRemove = function () {
      _this.setState({
        showStageDelete: false
      });
    };

    _this.closeCreateForm = function () {
      _this.setState({
        showHeadModal: false,
        stage: {}
      });
    };

    _this.openCreateForm = function () {
      var _this$props = _this.props,
          stageId = _this$props.stageId,
          clickAdd = _this$props.clickAdd;
      clickAdd(stageId);
    };

    return _this;
  }

  _createClass(StageCard, [{
    key: "openTaskRemove",
    value: function openTaskRemove(id, name, isHead) {
      this.setState({
        showTaskDelete: true,
        taskId: id,
        taskName: name,
        isRemoveHead: isHead
      });
    }
  }, {
    key: "renderTaskList",
    get: function get() {
      var _this2 = this;

      var _this$props2 = this.props,
          stageId = _this$props2.stageId,
          formatMessage = _this$props2.intl.formatMessage;
      return _.map(PipelineCreateStore.getTaskList[stageId], function (_ref) {
        var name = _ref.name,
            type = _ref.type,
            index = _ref.index,
            isHead = _ref.isHead;
        var isTaskTypeError = isHead && type === TASK_TYPE_MANUAL && PipelineCreateStore.getTrigger === TRIGGER_TYPE_AUTO;
        return /*#__PURE__*/React.createElement("div", {
          key: index,
          className: "c7ncd-stagecard-item ".concat(isTaskTypeError ? 'c7ncd-stagecard-error' : '')
        }, /*#__PURE__*/React.createElement(_Tooltip, {
          title: isTaskTypeError ? formatMessage({
            id: 'pipeline.mode.error'
          }, {
            name: name
          }) : name,
          placement: "top"
        }, /*#__PURE__*/React.createElement("span", {
          className: "c7ncd-stagecard-title"
        }, /*#__PURE__*/React.createElement(ClickText, {
          value: "\u3010".concat(formatMessage({
            id: "pipeline.mode.".concat(type)
          }), "\u3011").concat(name),
          onClick: _this2.openTaskSidebar.bind(_this2, null, index, isHead),
          clickAble: true
        }))), /*#__PURE__*/React.createElement(_Button, {
          onClick: _this2.openTaskRemove.bind(_this2, index, name, isHead),
          size: "small",
          icon: "delete_forever",
          shape: "circle"
        }));
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          stageId = _this$props3.stageId,
          allowDelete = _this$props3.allowDelete,
          formatMessage = _this$props3.intl.formatMessage,
          head = _this$props3.head;
      var _this$state2 = this.state,
          stage = _this$state2.stage,
          showTask = _this$state2.showTask,
          showTaskDelete = _this$state2.showTaskDelete,
          taskName = _this$state2.taskName,
          taskId = _this$state2.taskId,
          showStageDelete = _this$state2.showStageDelete,
          showHeadModal = _this$state2.showHeadModal,
          isEditHead = _this$state2.isEditHead;

      var _ref2 = _.find(PipelineCreateStore.getStageList, ['tempId', stageId]) || {},
          stageName = _ref2.stageName,
          triggerType = _ref2.triggerType,
          isParallel = _ref2.isParallel;

      var hasManualTask = _.find(PipelineCreateStore.getTaskList[stageId], ['type', 'manual']);

      return /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-stage-wrap"
      }, /*#__PURE__*/React.createElement(_Tooltip, {
        title: formatMessage({
          id: 'pipeline.stage.add'
        }),
        placement: "top"
      }, /*#__PURE__*/React.createElement(_Button, {
        className: "c7ncd-pipeline-create-btn",
        shape: "circle",
        onClick: this.openCreateForm
      }, /*#__PURE__*/React.createElement(_Icon, {
        type: "add",
        className: "c7ncd-pipeline-create-icon"
      }))), /*#__PURE__*/React.createElement(StageTitle, {
        head: head,
        allowDelete: allowDelete,
        name: stageName,
        type: triggerType,
        onChange: this.handleHeadChange,
        onRemove: this.openStageRemove
      }), /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-stagecard-wrap c7ncd-sidebar-select"
      }, /*#__PURE__*/React.createElement(_Select, {
        value: _.toString(isParallel || TASK_SERIAL),
        label: /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "pipeline.task.settings"
        }),
        onChange: this.handleSelect,
        className: "c7ncd-stage-card-select"
      }, /*#__PURE__*/React.createElement(Option, {
        value: _.toString(TASK_SERIAL)
      }, formatMessage({
        id: 'pipeline.task.serial'
      })), /*#__PURE__*/React.createElement(Option, {
        disabled: !!hasManualTask,
        value: _.toString(TASK_PARALLEL),
        title: hasManualTask ? formatMessage({
          id: 'pipeline.task.type.change'
        }) : ''
      }, formatMessage({
        id: 'pipeline.task.parallel'
      }))), /*#__PURE__*/React.createElement(Tips, {
        type: "form",
        data: "pipeline.task.serial.tip"
      }), /*#__PURE__*/React.createElement("h3", {
        className: "c7ncd-stagecard-label"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipeline.task.list"
      })), /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-stagecard-list"
      }, this.renderTaskList), /*#__PURE__*/React.createElement(_Button, {
        type: "primary",
        funcType: "flat",
        icon: "add",
        onClick: this.openTaskSidebar
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipeline.task.add"
      }))), /*#__PURE__*/React.createElement(_Modal, {
        visible: showTaskDelete,
        title: "".concat(formatMessage({
          id: 'pipeline.task.delete'
        }), "\u201C").concat(taskName, "\u201D"),
        closable: false,
        footer: [/*#__PURE__*/React.createElement(_Button, {
          key: "back",
          onClick: this.closeTaskRemove
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "cancel"
        })), /*#__PURE__*/React.createElement(_Button, {
          key: "submit",
          type: "danger",
          onClick: this.handleTaskDelete
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "delete"
        }))]
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7n-padding-top_8"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipeline.task.delete.msg"
      }))), /*#__PURE__*/React.createElement(_Modal, {
        visible: showStageDelete,
        title: "".concat(formatMessage({
          id: 'pipeline.stage.delete'
        }), "\u201C").concat(stageName, "\u201D"),
        closable: false,
        footer: [/*#__PURE__*/React.createElement(_Button, {
          key: "back",
          onClick: this.closeStageRemove
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "cancel"
        })), /*#__PURE__*/React.createElement(_Button, {
          key: "submit",
          type: "danger",
          onClick: this.handleStageRemove
        }, /*#__PURE__*/React.createElement(FormattedMessage, {
          id: "delete"
        }))]
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7n-padding-top_8"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "pipeline.stage.delete.msg"
      }))), showTask && /*#__PURE__*/React.createElement(TaskCreate, {
        id: taskId,
        isHead: isEditHead,
        stageName: stageName,
        stageId: stageId,
        visible: showTask,
        onClose: this.onCloseSidebar
      }), showHeadModal && /*#__PURE__*/React.createElement(StageCreateModal, {
        visible: showHeadModal,
        stage: stage,
        store: PipelineCreateStore,
        onClose: this.closeCreateForm
      }));
    }
  }]);

  return StageCard;
}(Component), _class2.defaultProps = {
  allowDelete: true
}, _temp)) || _class) || _class) || _class);
export { StageCard as default };