"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FindBar = undefined;

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _core = require("@blueprintjs/core");

var _withEditorProps = require("../withEditorProps");

var _withEditorProps2 = _interopRequireDefault(_withEditorProps);

var _onlyUpdateForKeysDeep = require("../utils/onlyUpdateForKeysDeep");

var _onlyUpdateForKeysDeep2 = _interopRequireDefault(_onlyUpdateForKeysDeep);

var _findToolConstants = require("../constants/findToolConstants");

require("./style.css");

var _teselagenReactComponents = require("teselagen-react-components");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var opts = [{ label: "DNA", value: "DNA" }, { label: "Amino Acids", value: "AA" }];

var FindBar = exports.FindBar = function (_React$Component) {
  _inherits(FindBar, _React$Component);

  function FindBar() {
    _classCallCheck(this, FindBar);

    return _possibleConstructorReturn(this, (FindBar.__proto__ || Object.getPrototypeOf(FindBar)).apply(this, arguments));
  }

  _createClass(FindBar, [{
    key: "render",
    value: function render() {
      var _props = this.props,
          toggleFindTool = _props.toggleFindTool,
          toggleHighlightAll = _props.toggleHighlightAll,
          updateSearchText = _props.updateSearchText,
          updateAmbiguousOrLiteral = _props.updateAmbiguousOrLiteral,
          updateDnaOrAA = _props.updateDnaOrAA,
          updateMatchNumber = _props.updateMatchNumber,
          isInline = _props.isInline,
          _props$findTool = _props.findTool,
          findTool = _props$findTool === undefined ? {} : _props$findTool;
      var searchText = findTool.searchText,
          dnaOrAA = findTool.dnaOrAA,
          highlightAll = findTool.highlightAll,
          ambiguousOrLiteral = findTool.ambiguousOrLiteral,
          _findTool$matchesTota = findTool.matchesTotal,
          matchesTotal = _findTool$matchesTota === undefined ? 0 : _findTool$matchesTota,
          _findTool$matchNumber = findTool.matchNumber,
          matchNumber = _findTool$matchNumber === undefined ? 0 : _findTool$matchNumber;

      var findOptionsEls = [_react2.default.createElement(_core.HTMLSelect, {
        key: "dnaoraa",
        options: opts,
        name: "dnaOrAA",
        value: dnaOrAA,
        onChange: function onChange(e) {
          updateDnaOrAA(e.target.value);
        }
      }), _react2.default.createElement(
        "div",
        { style: { display: "flex" }, key: "ambiguousorliteral" },
        _react2.default.createElement(_core.HTMLSelect, {
          name: "ambiguousOrLiteral",
          options: [{ label: "Literal", value: "LITERAL" }, { label: "Ambiguous", value: "AMBIGUOUS" }],
          value: ambiguousOrLiteral,
          onChange: function onChange(e) {
            updateAmbiguousOrLiteral(e.target.value);
          }
        }),
        _react2.default.createElement(
          _teselagenReactComponents.InfoHelper,
          { style: { marginLeft: 10 } },
          _react2.default.createElement(
            "div",
            null,
            "Ambiguous substitutions:",
            _react2.default.createElement(
              "div",
              { style: { display: "flex", fontSize: 12 } },
              _react2.default.createElement(
                "div",
                { style: { marginRight: 20 } },
                _react2.default.createElement(
                  "div",
                  { style: { fontSize: 14, marginBottom: 4, marginTop: 5 } },
                  "DNA:"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "M: AC"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "R: AG"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "W: AT"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "S: CG"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "Y: CT"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "K: GT"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "V: ACG"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "H: ACT"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "D: AGT"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "B: CGT"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "X: GATC"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "N: GATC"
                )
              ),
              _react2.default.createElement(
                "div",
                null,
                _react2.default.createElement(
                  "div",
                  { style: { fontSize: 14, marginBottom: 4, marginTop: 5 } },
                  "AA:"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "B: ND"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "J: IL"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "X: ACDEFGHIKLMNPQRSTVWY"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "Z: QE"
                ),
                _react2.default.createElement(
                  "div",
                  null,
                  "*: any"
                )
              )
            )
          )
        )
      ), _react2.default.createElement(
        _core.Switch,
        {
          key: "highlightall",
          checked: highlightAll,
          onChange: toggleHighlightAll,
          disabled: matchesTotal > _findToolConstants.MAX_MATCHES_DISPLAYED
        },
        "Highlight All",
        " ",
        matchesTotal > _findToolConstants.MAX_MATCHES_DISPLAYED && _react2.default.createElement(
          "div",
          null,
          "(Disabled because there are >",
          _findToolConstants.MAX_MATCHES_DISPLAYED,
          " matches)"
        )
      )];

      return _react2.default.createElement(
        "div",
        {
          style: isInline ? {
            display: "flex"
          } : {
            position: "fixed",
            top: 0,
            right: 25,
            padding: 10,
            display: "flex",
            alignItems: "center",
            paddingBottom: 5,
            background: "white",
            zIndex: "20000",
            borderBottom: "1px solid lightgrey",
            borderLeft: "1px solid lightgrey",
            borderRight: "1px solid lightgrey",
            borderBottomLeftRadius: "5px",
            borderBottomRightRadius: "5px"
          },
          className: "veFindBar"
        },
        _react2.default.createElement(_core.Button, { onClick: toggleFindTool, icon: "cross" }),
        _react2.default.createElement(_core.InputGroup, {
          autoFocus: true,
          onKeyDown: function onKeyDown(e) {
            e.persist();
            if (e.metaKey && e.keyCode === 70) {
              //cmd-f
              toggleFindTool();
              e.preventDefault();
              e.stopPropagation();
            } else if (e.keyCode === 13) {
              //enter key!
              updateMatchNumber(matchesTotal <= 0 ? 0 : mod(matchNumber + 1, matchesTotal));
            } else if (e.keyCode === 27) {
              //esc key!
              toggleFindTool();
            }
          },
          rightElement: _react2.default.createElement(
            "span",
            null,
            isInline && _react2.default.createElement(_core.Popover, {
              autoFocus: false,
              position: _core.Position.BOTTOM,
              target: _react2.default.createElement(_core.Button, {
                "data-test": "veFindBarOptionsToggle",
                minimal: true,
                icon: "wrench"
              }),
              content: _react2.default.createElement(
                "div",
                {
                  className: "ve-find-options-popover",
                  style: {
                    display: "flex",
                    flexDirection: "column",
                    paddingLeft: 20,
                    paddingBottom: 10,
                    paddingTop: 10,
                    paddingRight: 20
                  }
                },
                findOptionsEls
              )
            }),
            _react2.default.createElement(
              "span",
              { style: { marginRight: 3, color: "lightgrey" } },
              matchesTotal > 0 ? matchNumber + 1 : 0,
              "/",
              matchesTotal
            ),
            _react2.default.createElement(_core.Button, {
              "data-test": "veFindPreviousMatchButton",
              minimal: true,
              disabled: matchesTotal <= 0,
              onClick: function onClick() {
                updateMatchNumber(matchesTotal <= 0 ? 0 : mod(matchNumber - 1, matchesTotal));
              },
              icon: "caret-up"
            }),
            _react2.default.createElement(_core.Button, {
              "data-test": "veFindNextMatchButton",
              minimal: true,
              disabled: matchesTotal <= 0,
              onClick: function onClick() {
                updateMatchNumber(matchesTotal <= 0 ? 0 : mod(matchNumber + 1, matchesTotal));
              },
              icon: "caret-down"
            })
          ),
          onChange: function onChange(e) {
            return updateSearchText(e.target.value);
          },
          value: searchText,
          leftIcon: "search"
        }),
        !isInline && findOptionsEls
      );
    }
  }]);

  return FindBar;
}(_react2.default.Component);

var wrapped = (0, _onlyUpdateForKeysDeep2.default)(["findTool"])(FindBar);
exports.default = (0, _withEditorProps2.default)(wrapped);


function mod(n, m) {
  return (n % m + m) % m;
}