import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";

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

/* eslint-disable consistent-return */

/* eslint-disable max-len */
import React, { useEffect, useState } from 'react';
import { observer } from 'mobx-react-lite';
import map from 'lodash/map';
import forEach from 'lodash/forEach';
import jobTypesMappings from "../../../../stores/jobsTypeMappings";
import { usePipelineManageStore } from "../../../../stores";
import StageType from "../stage-type";
import Loading from "../../../../../../components/loading";
import "./index.less";
export default observer(function (props) {
  var _usePipelineManageSto = usePipelineManageStore(),
      getSelectedMenu = _usePipelineManageSto.mainStore.getSelectedMenu,
      projectId = _usePipelineManageSto.projectId,
      editBlockStore = _usePipelineManageSto.editBlockStore;

  var id = getSelectedMenu.id,
      name = getSelectedMenu.name,
      appServiceName = getSelectedMenu.appServiceName;

  var _useState = useState([]),
      _useState2 = _slicedToArray(_useState, 2),
      leftLineDom = _useState2[0],
      setLeftLineDom = _useState2[1];

  var _useState3 = useState([]),
      _useState4 = _slicedToArray(_useState3, 2),
      rightLineDom = _useState4[0],
      setRightLineDom = _useState4[1];

  var _ref = editBlockStore || {},
      loadData = _ref.loadData,
      getLoading = _ref.getLoading,
      getViewData = _ref.getViewData;

  useEffect(function () {
    id && loadData(projectId, id);
  }, [id, projectId]);
  useEffect(function () {
    var leftList = [];
    var rightList = [];
    forEach(getViewData, function (_ref2, stageIndex) {
      var jobList = _ref2.jobList;
      forEach(jobList, function (data, index) {
        var leftItem = /*#__PURE__*/React.createElement("div", {
          className: "c7ncd-pipeline-detail-job-rect-left",
          style: getJobRectStyle(stageIndex, index)
        });
        var rightItem = /*#__PURE__*/React.createElement("div", {
          className: "c7ncd-pipeline-detail-job-rect-right",
          style: getJobRectStyle(stageIndex, index)
        });

        if (leftList[stageIndex]) {
          leftList[stageIndex].push(leftItem);
          rightList[stageIndex].push(rightItem);
        } else {
          leftList[stageIndex] = [leftItem];
          rightList[stageIndex] = [rightItem];
        }
      });
    });
    setLeftLineDom(leftList);
    setRightLineDom(rightList);
  }, [getLoading, getViewData]);

  function getJobTask(_ref3) {
    var type = _ref3.jobType,
        metadata = _ref3.metadata,
        iamUserDTOS = _ref3.iamUserDTOS,
        jobTriggerValue = _ref3.jobTriggerValue,
        triggerValue = _ref3.triggerValue,
        envName = _ref3.envName,
        countersigned = _ref3.countersigned;

    if (type === 'cdAudit') {
      return /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-detail-job-task"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-detail-job-task-deploy"
      }, /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-pipeline-detail-job-task-deploy-item"
      }, "\u5BA1\u6838\u4EBA\u5458\uFF1A", map(iamUserDTOS || [], function (_ref4, index) {
        var loginName = _ref4.loginName,
            realName = _ref4.realName,
            userId = _ref4.id;
        return /*#__PURE__*/React.createElement("span", {
          key: userId
        }, realName, index !== iamUserDTOS.length - 1 && ',');
      })), /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-pipeline-detail-job-task-deploy-item"
      }, "\u5BA1\u6838\u6A21\u5F0F\uFF1A", countersigned === 0 ? '或签' : '会签'), /*#__PURE__*/React.createElement("span", null, "\u89E6\u53D1\u5206\u652F\uFF1A", jobTriggerValue === 'exact_exclude' ? '精确排除 ' : '', triggerValue || '所有分支或tag')));
    }

    if (metadata) {
      var newData = JSON.parse(metadata.replace(/'/g, '"'));

      var _ref5 = newData || {},
          sonarUrl = _ref5.sonarUrl,
          config = _ref5.config,
          scannerType = _ref5.scannerType,
          blockAfterJob = _ref5.blockAfterJob;

      var content;

      switch (type) {
        case 'sonar':
          if (sonarUrl) {
            content = /*#__PURE__*/React.createElement("div", {
              className: "c7ncd-pipeline-detail-job-task-sonar"
            }, /*#__PURE__*/React.createElement("span", null, sonarUrl), /*#__PURE__*/React.createElement("span", {
              className: "c7ncd-pipeline-detail-job-task-sonar-type"
            }, "\u68C0\u67E5\u7C7B\u578B:", /*#__PURE__*/React.createElement("br", null), scannerType));
          }

          break;

        case 'cdDeploy':
        case 'cdHost':
          content = /*#__PURE__*/React.createElement("div", {
            className: "c7ncd-pipeline-detail-job-task-deploy"
          }, envName ? /*#__PURE__*/React.createElement("span", {
            className: "c7ncd-pipeline-detail-job-task-deploy-item"
          }, "\u90E8\u7F72\u73AF\u5883\uFF1A", envName) : null, /*#__PURE__*/React.createElement("span", null, "\u89E6\u53D1\u5206\u652F\uFF1A", jobTriggerValue === 'exact_exclude' ? '精确排除 ' : '', triggerValue || '所有分支或tag'));
          break;

        case 'cdAudit':
          content = /*#__PURE__*/React.createElement("div", {
            className: "c7ncd-pipeline-detail-job-task-deploy"
          }, /*#__PURE__*/React.createElement("span", {
            className: "c7ncd-pipeline-detail-job-task-deploy-item"
          }, "\u5BA1\u6838\u4EBA\u5458\uFF1A", map(iamUserDTOS || [], function (_ref6, index) {
            var loginName = _ref6.loginName,
                realName = _ref6.realName,
                userId = _ref6.id;
            return /*#__PURE__*/React.createElement("span", {
              key: userId
            }, realName, index !== iamUserDTOS.length - 1 && ',');
          })), /*#__PURE__*/React.createElement("span", {
            className: "c7ncd-pipeline-detail-job-task-deploy-item"
          }, "\u5BA1\u6838\u6A21\u5F0F\uFF1A", countersigned === 0 ? '或签' : '会签'), /*#__PURE__*/React.createElement("span", null, "\u89E6\u53D1\u5206\u652F\uFF1A", jobTriggerValue === 'exact_exclude' ? '精确排除 ' : '', triggerValue || '所有分支或tag'));
          break;

        case 'build':
          content = config ? map(config, function (_ref7) {
            var taskName = _ref7.name,
                sequence = _ref7.sequence;
            return /*#__PURE__*/React.createElement("div", {
              className: "c7ncd-pipeline-detail-job-task-item",
              key: sequence
            }, taskName);
          }) : null;
          break;

        case 'cdApiTest':
          content = /*#__PURE__*/React.createElement("div", {
            className: "c7ncd-pipeline-detail-job-task-deploy"
          }, /*#__PURE__*/React.createElement("span", {
            className: "c7ncd-pipeline-detail-job-task-deploy-item"
          }, "\u662F\u5426\u963B\u585E\uFF1A", blockAfterJob ? '是' : '否'), /*#__PURE__*/React.createElement("span", null, "\u89E6\u53D1\u5206\u652F\uFF1A", jobTriggerValue === 'exact_exclude' ? '精确排除 ' : '', triggerValue || '所有分支或tag'));

        default:
      }

      return content && /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-detail-job-task"
      }, content);
    }
  }

  function getJobRectStyle(stageIndex, index) {
    var sum = 0;

    for (var i = 0; i < index; i += 1) {
      sum += document.getElementById("".concat(id, "-").concat(stageIndex, "-job-").concat(i)) ? document.getElementById("".concat(id, "-").concat(stageIndex, "-job-").concat(i)).offsetHeight + 32 : 0;
    }

    return {
      height: sum
    };
  }

  return !getLoading ? /*#__PURE__*/React.createElement("div", {
    className: "c7ncd-pipeline-detail"
  }, /*#__PURE__*/React.createElement("div", {
    className: "c7ncd-pipeline-detail-title"
  }, /*#__PURE__*/React.createElement("span", null, name), /*#__PURE__*/React.createElement("span", {
    className: "c7ncd-pipeline-detail-title-appService"
  }, appServiceName ? " (".concat(appServiceName, ") ") : '')), /*#__PURE__*/React.createElement("div", {
    className: "c7ncd-pipeline-detail-content"
  }, map(getViewData, function (_ref8, stageIndex) {
    var stageId = _ref8.id,
        stageName = _ref8.name,
        jobList = _ref8.jobList,
        _ref8$type = _ref8.type,
        stageType = _ref8$type === void 0 ? 'CI' : _ref8$type,
        parallel = _ref8.parallel,
        _ref8$triggerType = _ref8.triggerType,
        triggerType = _ref8$triggerType === void 0 ? 'auto' : _ref8$triggerType;
    return /*#__PURE__*/React.createElement("div", {
      className: "c7ncd-pipeline-detail-stage",
      key: "".concat(stageId, "-").concat(stageIndex)
    }, /*#__PURE__*/React.createElement("div", {
      className: "c7ncd-pipeline-detail-stage-title"
    }, /*#__PURE__*/React.createElement(_Tooltip, {
      title: stageName,
      placement: "top"
    }, /*#__PURE__*/React.createElement("span", {
      className: "c7ncd-pipeline-detail-stage-title-text"
    }, stageName)), /*#__PURE__*/React.createElement("div", {
      className: "c7ncd-pipeline-detail-stage-title-type"
    }, /*#__PURE__*/React.createElement(StageType, {
      type: stageType,
      parallel: parallel
    }))), /*#__PURE__*/React.createElement("div", {
      className: "c7ncd-pipeline-detail-stage-line"
    }), stageIndex !== 0 ? /*#__PURE__*/React.createElement("div", {
      className: "c7ncd-pipeline-detail-stage-arrow"
    }, triggerType === 'auto' ? /*#__PURE__*/React.createElement("svg", {
      xmlns: "http://www.w3.org/2000/svg",
      width: "28",
      height: "9",
      viewBox: "0 0 28 9"
    }, /*#__PURE__*/React.createElement("path", {
      fill: "#6887E8",
      d: "M511.5,131 L520.5,135.5 L511.5,140 L511.5,136 L493,136 L493,135 L511.5,135 L511.5,131 Z",
      transform: "translate(-493 -131)"
    })) : /*#__PURE__*/React.createElement("svg", {
      xmlns: "http://www.w3.org/2000/svg",
      width: "28",
      height: "9",
      viewBox: "0 0 26 9"
    }, /*#__PURE__*/React.createElement("path", {
      fill: "#F1B42D",
      d: "M917.5,130 L926.5,134.5 L917.5,139 L917.5,135 L913.5,135 L913.5,134 L917.5,134 L917.5,130 Z M905.5,134 L905.5,135 L901.5,135 L901.5,134 L905.5,134 Z M911.5,134 L911.5,135 L907.5,135 L907.5,134 L911.5,134 Z",
      transform: "translate(-901 -130)"
    }))) : null, map(jobList, function (_ref9, index) {
      var jobId = _ref9.id,
          jobType = _ref9.type,
          jobName = _ref9.name,
          metadata = _ref9.metadata,
          iamUserDTOS = _ref9.iamUserDTOS,
          jobTriggerValue = _ref9.triggerType,
          triggerValue = _ref9.triggerValue,
          envName = _ref9.envName,
          countersigned = _ref9.countersigned;
      return /*#__PURE__*/React.createElement("div", {
        key: "".concat(stageId, "-").concat(jobId)
      }, index && leftLineDom[stageIndex] ? leftLineDom[stageIndex][index] : null, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-detail-job c7ncd-pipeline-detail-job-".concat(stageType),
        id: "".concat(id, "-").concat(stageIndex, "-job-").concat(index)
      }, /*#__PURE__*/React.createElement(_Tooltip, {
        title: "\u3010".concat(jobTypesMappings[jobType], "\u3011").concat(jobName),
        placement: "top"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pipeline-detail-job-title"
      }, "\u3010", jobTypesMappings[jobType], "\u3011", jobName)), jobType !== 'custom' && getJobTask({
        jobType: jobType,
        metadata: metadata,
        iamUserDTOS: iamUserDTOS,
        jobTriggerValue: jobTriggerValue,
        triggerValue: triggerValue,
        envName: envName,
        countersigned: countersigned
      })), index && stageIndex !== getViewData.length - 1 && rightLineDom[stageIndex] ? rightLineDom[stageIndex][index] : null);
    }));
  }))) : /*#__PURE__*/React.createElement(Loading, {
    display: true
  });
});