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 = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useEffect, useState } from 'react';
import { Radio, Input, Form, Row, Col, message, InputNumber } from 'antd';
import PisellModal from "../pisellModal";
import VersionSelect from "../versionSelect";
import { getText } from "../../locales";
import "./index.less";
var style = {
  display: 'flex',
  flexDirection: 'column',
  gap: 8
};
var parseVersion = function parseVersion(version) {
  var _version$split$map = version.split('.').map(Number),
    _version$split$map2 = _slicedToArray(_version$split$map, 3),
    major = _version$split$map2[0],
    minor = _version$split$map2[1],
    patch = _version$split$map2[2];
  return [major || 0, minor || 0, patch || 0];
};
var isVersionValid = function isVersionValid(customVersion, currentVersion) {
  var _parseVersion = parseVersion(customVersion),
    _parseVersion2 = _slicedToArray(_parseVersion, 3),
    cMajor = _parseVersion2[0],
    cMinor = _parseVersion2[1],
    cPatch = _parseVersion2[2];
  var _parseVersion3 = parseVersion(currentVersion),
    _parseVersion4 = _slicedToArray(_parseVersion3, 3),
    liveMajor = _parseVersion4[0],
    liveMinor = _parseVersion4[1],
    livePatch = _parseVersion4[2];

  // 版本号必须大于当前版本
  if (cMajor > liveMajor) return true;
  if (cMajor === liveMajor && cMinor > liveMinor) return true;
  if (cMajor === liveMajor && cMinor === liveMinor && cPatch > livePatch) return true;
  return false;
};
var getNextVersions = function getNextVersions(version) {
  var _parseVersion5 = parseVersion(version),
    _parseVersion6 = _slicedToArray(_parseVersion5, 3),
    major = _parseVersion6[0],
    minor = _parseVersion6[1],
    patch = _parseVersion6[2];
  return {
    patch: "".concat(major, ".").concat(minor, ".").concat(patch + 1),
    minor: "".concat(major, ".").concat(minor + 1, ".0"),
    major: "".concat(major + 1, ".0.0")
  };
};
var PublishVersionModal = function PublishVersionModal(_ref) {
  var visible = _ref.visible,
    _ref$currentVersion = _ref.currentVersion,
    currentVersion = _ref$currentVersion === void 0 ? '1.0.0' : _ref$currentVersion,
    onCancel = _ref.onCancel,
    onConfirm = _ref.onConfirm,
    isShowNote = _ref.isShowNote,
    _ref$confirmLoading = _ref.confirmLoading,
    confirmLoading = _ref$confirmLoading === void 0 ? false : _ref$confirmLoading,
    _ref$versions = _ref.versions,
    versions = _ref$versions === void 0 ? [] : _ref$versions,
    isShowBasedOn = _ref.isShowBasedOn,
    isShowCurrentVersion = _ref.isShowCurrentVersion,
    _ref$isValidateVersio = _ref.isValidateVersion,
    isValidateVersion = _ref$isValidateVersio === void 0 ? true : _ref$isValidateVersio;
  var _Form$useForm = Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    versionType = _useState2[0],
    setVersionType = _useState2[1];
  var _getNextVersions = getNextVersions(currentVersion),
    patch = _getNextVersions.patch,
    minor = _getNextVersions.minor,
    major = _getNextVersions.major;
  var handleVersionChange = function handleVersionChange(e) {
    setVersionType(e.target.value);
  };
  var handleSubmit = function handleSubmit() {
    form.validateFields().then(function (values) {
      var version = values.version,
        note = values.note,
        baseVersion = values.baseVersion;
      var versionType = '';
      if (version === 'custom') {
        versionType = 'custom';
        if (isValidateVersion && !isVersionValid("".concat(values.major, ".").concat(values.minor, ".").concat(values.patch), currentVersion)) {
          message.error(getText('pisell-version-modal-version-error'));
          return;
        }
        version = "".concat(values.major, ".").concat(values.minor, ".").concat(values.patch);
      }
      onConfirm({
        version: version,
        note: note,
        baseVersion: baseVersion,
        versionType: versionType
      });
    });
  };
  useEffect(function () {
    if (visible) {
      setVersionType('');
      form.setFieldsValue({
        version: patch,
        major: parseVersion(patch)[0] || 0,
        minor: parseVersion(patch)[1] || 0,
        patch: parseVersion(patch)[2] || 0,
        baseVersion: currentVersion
      });
    }
  }, [visible]);
  return /*#__PURE__*/React.createElement(PisellModal, {
    title: getText('pisell-version-modal-title'),
    open: visible,
    onCancel: onCancel,
    destroyOnClose: true,
    onOk: handleSubmit,
    okText: getText('pisell-version-modal-confirm'),
    cancelText: getText('pisell-version-modal-cancel'),
    confirmLoading: confirmLoading
  }, /*#__PURE__*/React.createElement(Form, {
    form: form,
    layout: "vertical",
    preserve: false,
    className: "pisell-lowcode-version-modal-form"
  }, isShowCurrentVersion && /*#__PURE__*/React.createElement("p", {
    className: "pisell-lowcode-version-modal-current-version"
  }, getText('pisell-version-modal-current-version'), ": ", currentVersion), isShowBasedOn && /*#__PURE__*/React.createElement(Form.Item, {
    label: getText('pisell-version-modal-based-on-version'),
    name: "baseVersion",
    initialValue: currentVersion,
    rules: [{
      required: true
    }]
  }, /*#__PURE__*/React.createElement(VersionSelect, {
    versions: versions,
    fieldNames: {
      label: 'label',
      value: 'version',
      options: 'options'
    }
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: getText('pisell-version-modal-version'),
    name: "version",
    initialValue: "patch",
    rules: [{
      required: true
    }]
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    onChange: handleVersionChange,
    style: style
  }, /*#__PURE__*/React.createElement(Radio, {
    value: patch
  }, getText('pisell-version-modal-patch'), " (", patch, ")"), /*#__PURE__*/React.createElement(Radio, {
    value: minor
  }, getText('pisell-version-modal-minor'), " (", minor, ")"), /*#__PURE__*/React.createElement(Radio, {
    value: major
  }, getText('pisell-version-modal-major'), " (", major, ")"), /*#__PURE__*/React.createElement(Radio, {
    value: "custom"
  }, getText('pisell-version-modal-custom')))), versionType === 'custom' && /*#__PURE__*/React.createElement(Row, {
    gutter: 8
  }, /*#__PURE__*/React.createElement(Col, {
    span: 8
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: "major",
    label: getText('pisell-version-modal-major'),
    rules: [{
      required: true,
      message: getText('pisell-version-modal-version-required')
    }]
  }, /*#__PURE__*/React.createElement(InputNumber, {
    min: 0,
    precision: 0,
    style: {
      width: '100%'
    },
    placeholder: getText('pisell-version-modal-major')
  }))), /*#__PURE__*/React.createElement(Col, {
    span: 8
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: "minor",
    label: getText('pisell-version-modal-minor'),
    rules: [{
      required: true,
      message: getText('pisell-version-modal-version-required')
    }]
  }, /*#__PURE__*/React.createElement(InputNumber, {
    precision: 0,
    min: 0,
    style: {
      width: '100%'
    },
    placeholder: getText('pisell-version-modal-minor')
  }))), /*#__PURE__*/React.createElement(Col, {
    span: 8
  }, /*#__PURE__*/React.createElement(Form.Item, {
    name: "patch",
    label: getText('pisell-version-modal-patch'),
    rules: [{
      required: true,
      message: getText('pisell-version-modal-version-required')
    }]
  }, /*#__PURE__*/React.createElement(InputNumber, {
    min: 0,
    precision: 0,
    style: {
      width: '100%'
    },
    placeholder: getText('pisell-version-modal-patch')
  })))), isShowNote && /*#__PURE__*/React.createElement(Form.Item, {
    label: getText('pisell-version-modal-update-notes'),
    name: "note"
  }, /*#__PURE__*/React.createElement(Input.TextArea, {
    placeholder: getText('pisell-version-modal-describe-update-notes')
  }))));
};
export default PublishVersionModal;