"use strict";

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

var _extends = Object.assign || 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; };

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

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 _teselagenReactComponents = require("teselagen-react-components");

var _lodash = require("lodash");

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

var _veRangeUtils = require("ve-range-utils");

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

var _recompose = require("recompose");

var _commands = require("../../commands");

var _commands2 = _interopRequireDefault(_commands);

var _utils = require("./utils");

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 PartProperties = function (_React$Component) {
  _inherits(PartProperties, _React$Component);

  function PartProperties(props) {
    _classCallCheck(this, PartProperties);

    var _this = _possibleConstructorReturn(this, (PartProperties.__proto__ || Object.getPrototypeOf(PartProperties)).call(this, props));

    _this.onRowSelect = function (_ref) {
      var _ref2 = _slicedToArray(_ref, 1),
          record = _ref2[0];

      if (!record) return;
      var _this$props = _this.props,
          dispatch = _this$props.dispatch,
          editorName = _this$props.editorName;

      dispatch({
        type: "SELECTION_LAYER_UPDATE",
        payload: record,
        meta: {
          editorName: editorName
        }
      });
    };

    _this.commands = (0, _commands2.default)(_this);
    return _this;
  }

  _createClass(PartProperties, [{
    key: "render",
    value: function render() {
      var _props = this.props,
          readOnly = _props.readOnly,
          parts = _props.parts,
          sequenceLength = _props.sequenceLength,
          partPropertiesSelectedEntities = _props.partPropertiesSelectedEntities,
          showAddOrEditPartDialog = _props.showAddOrEditPartDialog,
          deletePart = _props.deletePart,
          isProtein = _props.isProtein,
          annotationVisibility = _props.annotationVisibility,
          selectedAnnotationId = _props.selectedAnnotationId;

      var partsToUse = (0, _lodash.map)(parts, function (part) {
        return _extends({}, part, part.strand === undefined && {
          strand: part.forward ? 1 : -1
        }, {
          size: (0, _veRangeUtils.getRangeLength)(part, sequenceLength)
        });
      });
      return _react2.default.createElement(
        "div",
        { style: { display: "flex", flexDirection: "column" } },
        _react2.default.createElement(_teselagenReactComponents.DataTable, {
          topLeftItems: _react2.default.createElement(_teselagenReactComponents.CmdCheckbox, { prefix: "Show ", cmd: this.commands.toggleParts }),
          annotationVisibility: annotationVisibility //we need to pass this in order to force the DT to rerender
          , noPadding: true,
          noFullscreenButton: true,
          onRowSelect: this.onRowSelect,
          selectedIds: selectedAnnotationId,
          maxHeight: 400,
          formName: "partProperties",
          noRouter: true,
          compact: true,
          isProtein: isProtein,
          isInfinite: true,
          schema: {
            fields: [{ path: "name", type: "string" }, _utils.sizeSchema, { path: "strand", type: "string" }]
          },
          entities: partsToUse
        }),
        !readOnly && _react2.default.createElement(
          "div",
          { className: "vePropertiesFooter" },
          _react2.default.createElement(
            _core.Button,
            {
              style: { marginRight: 15 },
              onClick: function onClick() {
                showAddOrEditPartDialog();
              }
            },
            "New"
          ),
          _react2.default.createElement(
            _core.Button,
            {
              onClick: function onClick() {
                showAddOrEditPartDialog(partPropertiesSelectedEntities[0]);
              },
              style: { marginRight: 15 },
              disabled: partPropertiesSelectedEntities.length !== 1
            },
            "Edit"
          ),
          _react2.default.createElement(
            _core.Button,
            {
              onClick: function onClick() {
                deletePart(partPropertiesSelectedEntities);
              },
              style: { marginRight: 15 },
              disabled: !partPropertiesSelectedEntities.length
            },
            "Delete"
          )
        )
      );
    }
  }]);

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

exports.default = (0, _recompose.compose)((0, _withEditorProps.connectToEditor)(function (_ref3) {
  var readOnly = _ref3.readOnly,
      _ref3$annotationVisib = _ref3.annotationVisibility,
      annotationVisibility = _ref3$annotationVisib === undefined ? {} : _ref3$annotationVisib,
      _ref3$sequenceData = _ref3.sequenceData;
  _ref3$sequenceData = _ref3$sequenceData === undefined ? {} : _ref3$sequenceData;
  var _ref3$sequenceData$se = _ref3$sequenceData.sequence,
      sequence = _ref3$sequenceData$se === undefined ? "" : _ref3$sequenceData$se,
      _ref3$sequenceData$pa = _ref3$sequenceData.parts,
      parts = _ref3$sequenceData$pa === undefined ? {} : _ref3$sequenceData$pa;

  return {
    readOnly: readOnly,
    parts: parts,
    annotationVisibility: annotationVisibility,
    sequenceLength: sequence.length
  };
}), (0, _teselagenReactComponents.withSelectedEntities)("partProperties"))(PartProperties);
module.exports = exports["default"];