"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _teselagenReactComponents = require("teselagen-react-components");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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); }
var Mismatches = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(Mismatches, _React$Component);
  function Mismatches() {
    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), "getGapMap", function (sequence) {
      var gapMap = [0]; //a map of position to how many gaps come before that position [0,0,0,5,5,5,5,17,17,17, ]
      sequence.split("").forEach(function (_char) {
        if (_char === "-") {
          gapMap[Math.max(0, gapMap.length - 1)] = (gapMap[Math.max(0, gapMap.length - 1)] || 0) + 1;
        } else {
          gapMap.push(gapMap[gapMap.length - 1] || 0);
        }
      });
      return gapMap;
    });
    _defineProperty(_assertThisInitialized(_this), "getMismatchList", function (alignmentData, mismatches) {
      // getMismatchList = (alignmentId, alignments) => {
      // let mismatchListAll = [];
      // skip first sequence/ref seq, since there will be no mismatches
      // for (let trackI = 1; trackI < alignments[alignmentId].alignmentTracks.length; trackI++) {
      var mismatchList = [];
      var trackName = alignmentData.name;
      var editedTrackName = trackName.slice(trackName.indexOf("_") + 1);
      var getGaps = function getGaps() {
        return {
          gapsBefore: 0,
          gapsInside: 0
        };
      };
      var gapMap = _this.getGapMap(alignmentData.sequence);
      getGaps = function getGaps(rangeOrCaretPosition) {
        if (typeof rangeOrCaretPosition !== "object") {
          return {
            gapsBefore: gapMap[Math.min(rangeOrCaretPosition, gapMap.length - 1)]
          };
        }
        var start = rangeOrCaretPosition.start,
          end = rangeOrCaretPosition.end;
        var toReturn = {
          gapsBefore: gapMap[start],
          gapsInside: gapMap[Math.min(end, gapMap.length - 1)] - gapMap[Math.min(start, gapMap.length - 1)]
        };
        return toReturn;
      };
      var gapsBeforeSequence = getGaps(0).gapsBefore;
      for (var mismatchI = 0; mismatchI < mismatches.length; mismatchI++) {
        var mismatchEnd = mismatches[mismatchI].end;
        var mismatchStart = mismatches[mismatchI].start;
        var mismatchDifference = mismatchEnd - mismatchStart;
        // display 'position' as 1-based but store 'start' & 'end' as 0-based
        if (mismatchDifference === 0) {
          mismatchList.push({
            mismatches: mismatchStart + 1 - gapsBeforeSequence,
            start: mismatchStart - gapsBeforeSequence,
            end: mismatchStart - gapsBeforeSequence
          });
        } else {
          for (var innerI = 0; innerI <= mismatchDifference; innerI++) {
            mismatchList.push({
              mismatches: mismatchStart + innerI + 1 - gapsBeforeSequence,
              start: mismatchStart + innerI - gapsBeforeSequence,
              end: mismatchStart + innerI - gapsBeforeSequence
            });
          }
        }
      }
      return mismatchList;
    });
    return _this;
  }
  var _proto = Mismatches.prototype;
  _proto.UNSAFE_componentWillMount = function UNSAFE_componentWillMount() {
    var _this$props = this.props,
      alignmentData = _this$props.alignmentData,
      mismatches = _this$props.mismatches;
    // const { alignmentId, alignments } = this.props;
    var mismatchList = this.getMismatchList(alignmentData, mismatches);
    // const mismatchListAll = this.getMismatchList(alignmentId, alignments);
    var schema = {
      fields: [{
        path: "mismatches",
        type: "number"
      }]
    };
    this.setState({
      mismatchList: mismatchList,
      schema: schema
    });
  };
  _proto.render = function render() {
    var _this$state = this.state,
      mismatchList = _this$state.mismatchList,
      schema = _this$state.schema;
    var tableOfMismatches;
    if (mismatchList.length === 0) {
      tableOfMismatches = null;
    } else {
      tableOfMismatches = /*#__PURE__*/_react["default"].createElement(_teselagenReactComponents.DataTable, {
        maxHeight: 168,
        formName: "mismatchesTable",
        isSimple: true,
        compact: true,
        noRouter: true
        // onRowSelect={this.handleMismatchClick}
        ,
        schema: schema,
        entities: mismatchList
      });
    }
    return /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        maxHeight: 180.8,
        overflowY: "scroll"
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        // margin: 10,
        display: "flex",
        flexDirection: "column",
        alignItems: "center"
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        width: 100,
        margin: 4
      }
    }, tableOfMismatches)));
  };
  return Mismatches;
}(_react["default"].Component);
var _default = (0, _teselagenReactComponents.withSelectedEntities)("mismatchesTable")(Mismatches);
exports["default"] = _default;
module.exports = exports.default;