function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { convertRangeTo0Based } from "@teselagen/range-utils";
import classNames from "classnames";
import React from "react";
import { reduxForm } from "redux-form";
import { wrapDialog } from "teselagen-react-components";
import { compose } from "redux";
import { Button, Intent, Classes } from "@blueprintjs/core";
import { NumericInputField } from "teselagen-react-components";
import { get } from "lodash";
import { getRangeLength } from "@teselagen/range-utils";
import { tryToRefocusEditor } from "../utils/editorUtils";
import tgFormValues from "../utils/tgFormValues";

// Single validation function - from & to have the same range
var validate = function validate(val, vals, props) {
  var _get = get(props, "extraProps.from", {}),
    min = _get.min,
    max = _get.max;
  var circular = get(props, "extraProps.circular");
  if (min && val < min || max && val > max) {
    return "Invalid position";
  }
  if (!circular && Number(vals.from) > Number(vals.to)) {
    return "Wrong from/to order";
  }
};
// const selector = formValueSelector("selectDialog");
export default compose(wrapDialog({
  isDraggable: true,
  width: 400,
  title: "Select Range",
  height: 270,
  onCloseHook: tryToRefocusEditor
}), reduxForm({
  form: "selectDialog"
}), tgFormValues("from", "to"))( /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(SelectDialog, _React$Component);
  function SelectDialog() {
    var _this;
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
    _defineProperty(_assertThisInitialized(_this), "updateTempHighlight", function (_temp) {
      var _ref = _temp === void 0 ? {} : _temp,
        isStart = _ref.isStart,
        isEnd = _ref.isEnd;
      return function (val) {
        var _this$props = _this.props,
          selectionLayerUpdate = _this$props.selectionLayerUpdate,
          from = _this$props.from,
          to = _this$props.to,
          invalid = _this$props.invalid;
        if (invalid) return;
        selectionLayerUpdate(convertRangeTo0Based({
          start: isStart ? Math.round(val) : from,
          end: isEnd ? Math.round(val) : to
        }));
      };
    });
    return _this;
  }
  var _proto = SelectDialog.prototype;
  _proto.componentDidMount = function componentDidMount() {
    var _this$props2 = this.props,
      from = _this$props2.from,
      to = _this$props2.to,
      initialCaretPosition = _this$props2.initialCaretPosition;
    this.initialSelection = {
      from: from,
      to: to,
      initialCaretPosition: initialCaretPosition
    };
    this.updateTempHighlight()();
  };
  _proto.render = function render() {
    var _this2 = this;
    var _this$props3 = this.props,
      hideModal = _this$props3.hideModal,
      onSubmit = _this$props3.onSubmit,
      selectionLayerUpdate = _this$props3.selectionLayerUpdate,
      from = _this$props3.from,
      to = _this$props3.to,
      initialCaretPosition = _this$props3.initialCaretPosition,
      caretPositionUpdate = _this$props3.caretPositionUpdate,
      sequenceLength = _this$props3.sequenceLength,
      extraProps = _this$props3.extraProps,
      isProtein = _this$props3.isProtein,
      invalid = _this$props3.invalid,
      handleSubmit = _this$props3.handleSubmit;
    var selectionLength = getRangeLength({
      start: Number(from),
      end: Number(to)
    }, sequenceLength);
    return /*#__PURE__*/React.createElement("form", {
      onSubmit: handleSubmit(function (data) {
        if (onSubmit) onSubmit(data);
        hideModal();
        tryToRefocusEditor();
      }),
      className: classNames(Classes.DIALOG_BODY, "tg-min-width-dialog simple-dialog")
    }, /*#__PURE__*/React.createElement(NumericInputField, _extends({
      autoFocus: true,
      minorStepSize: 1,
      label: "From:",
      clampValueOnBlur: true
    }, extraProps.to, {
      validate: validate
      //tnrtodo this normalization will actually work when https://github.com/palantir/blueprint/issues/3553 gets resolved
      ,
      normalize: normalizeToInt,
      onAnyNumberChange: this.updateTempHighlight({
        isStart: true
      }),
      name: "from"
    })), /*#__PURE__*/React.createElement(NumericInputField, _extends({
      label: "To:",
      clampValueOnBlur: true,
      minorStepSize: 1
    }, extraProps.from, {
      validate: validate,
      normalize: normalizeToInt,
      onAnyNumberChange: this.updateTempHighlight({
        isEnd: true
      }),
      name: "to"
    })), /*#__PURE__*/React.createElement("div", {
      className: "dialog-buttons"
    }, /*#__PURE__*/React.createElement(Button, {
      onClick: function onClick() {
        if (initialCaretPosition > -1) {
          caretPositionUpdate(initialCaretPosition);
        } else {
          selectionLayerUpdate({
            start: _this2.initialSelection.from,
            end: _this2.initialSelection.to
          });
        }
        hideModal();
        tryToRefocusEditor();
      },
      text: "Cancel"
    }), /*#__PURE__*/React.createElement(Button, {
      type: "submit",
      intent: Intent.PRIMARY,
      text: "Select " + (invalid ? 0 : selectionLength) + " " + (isProtein ? "AA" : "BP") + (selectionLength === 1 ? "" : "s"),
      disabled: invalid
    })));
  };
  return SelectDialog;
}(React.Component));
var normalizeToInt = function normalizeToInt(val) {
  var _int = Math.round(val);
  var normalizedVal = "" + (_int >= 0 ? _int : 1);
  return normalizedVal;
};