"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 _ColorPicker = require("./ColorPicker");

var _ColorPicker2 = _interopRequireDefault(_ColorPicker);

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

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

    var _this = _possibleConstructorReturn(this, (FeatureProperties.__proto__ || Object.getPrototypeOf(FeatureProperties)).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(FeatureProperties, [{
    key: "render",
    value: function render() {
      var _props = this.props,
          readOnly = _props.readOnly,
          _props$features = _props.features,
          features = _props$features === undefined ? {} : _props$features,
          annotationVisibility = _props.annotationVisibility,
          sequenceLength = _props.sequenceLength,
          isProtein = _props.isProtein,
          featurePropertiesSelectedEntities = _props.featurePropertiesSelectedEntities,
          showAddOrEditFeatureDialog = _props.showAddOrEditFeatureDialog,
          deleteFeature = _props.deleteFeature,
          selectedAnnotationId = _props.selectedAnnotationId;


      var featuresToUse = (0, _lodash.map)(features, function (feature) {
        return _extends({}, feature, feature.strand === undefined && {
          strand: feature.forward ? 1 : -1
        }, {
          size: (0, _veRangeUtils.getRangeLength)(feature, sequenceLength)
        });
      });
      return _react2.default.createElement(
        _react2.default.Fragment,
        null,
        _react2.default.createElement(_teselagenReactComponents.DataTable, {
          topLeftItems: _react2.default.createElement(_teselagenReactComponents.CmdCheckbox, { prefix: "Show ", cmd: this.commands.toggleFeatures }),
          annotationVisibility: annotationVisibility //we need to pass this in order to force the DT to rerenderannotationVisibility={annotationVisibility}
          , noPadding: true,
          noFullscreenButton: true,
          onRowSelect: this.onRowSelect,
          maxHeight: 400,
          selectedIds: selectedAnnotationId,
          formName: "featureProperties",
          noRouter: true,
          isProtein: isProtein,
          compact: true,
          isInfinite: true,
          schema: {
            fields: [{
              path: "color",
              type: "string",
              render: function render(color) {
                return _react2.default.createElement(
                  ColorPickerPopover,
                  null,
                  _react2.default.createElement("div", {
                    style: { height: 20, width: 20, background: color }
                  })
                );
              }
            }, { path: "name", type: "string" }, { path: "type", type: "string" }, _utils.sizeSchema, { path: "strand", type: "string" }]
          },
          entities: featuresToUse
        }),
        !readOnly && _react2.default.createElement(
          "div",
          { className: "vePropertiesFooter" },
          _react2.default.createElement(
            _core.Button,
            {
              style: { marginRight: 15 },
              onClick: function onClick() {
                showAddOrEditFeatureDialog();
              }
            },
            "New"
          ),
          _react2.default.createElement(
            _core.Button,
            {
              onClick: function onClick() {
                showAddOrEditFeatureDialog(featurePropertiesSelectedEntities[0]);
              },
              style: { marginRight: 15 },
              disabled: featurePropertiesSelectedEntities.length !== 1
            },
            "Edit"
          ),
          _react2.default.createElement(
            _core.Button,
            {
              onClick: function onClick() {
                deleteFeature(featurePropertiesSelectedEntities);
              },
              style: { marginRight: 15 },
              disabled: !featurePropertiesSelectedEntities.length
            },
            "Delete"
          )
        )
      );
    }
  }]);

  return FeatureProperties;
}(_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$fe = _ref3$sequenceData.features,
      features = _ref3$sequenceData$fe === undefined ? {} : _ref3$sequenceData$fe;

  return {
    annotationVisibility: annotationVisibility,
    readOnly: readOnly,
    features: features,
    sequenceLength: sequence.length
  };
}), (0, _teselagenReactComponents.withSelectedEntities)("featureProperties"))(FeatureProperties);


var ColorPickerPopover = function ColorPickerPopover(_ref4) {
  var readOnly = _ref4.readOnly,
      onColorSelect = _ref4.onColorSelect,
      children = _ref4.children;

  return _react2.default.createElement(
    _core.Popover,
    {
      disabled: readOnly,
      content: _react2.default.createElement(_ColorPicker2.default, { onColorSelect: onColorSelect })
    },
    children
  );
};
module.exports = exports["default"];