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 React from "react";
import { reduxForm } from "redux-form";
import { wrapDialog, DataTable, withSelectedEntities, SwitchField } from "teselagen-react-components";
import { compose } from "redux";
import { Button, Classes, Popover } from "@blueprintjs/core";
import classNames from "classnames";
import withEditorProps from "../../withEditorProps";
import { forEach, camelCase, startCase } from "lodash";
import { sizeSchema } from "../PropertiesDialog/utils";
import { getRangeLength } from "@teselagen/range-utils";
import tgFormValues from "../../utils/tgFormValues";
var schema = {
  fields: [
  // ...(noColor
  //   ? []
  //   : [
  //       {
  //         path: "color",
  //         type: "string",
  //         render: color => {
  //           return (
  //             <ColorPickerPopover>
  //               <div style={{ height: 20, width: 20, background: color }} />
  //             </ColorPickerPopover>
  //           );
  //         }
  //       }
  //     ]),
  {
    path: "name",
    displayName: "名称",
    type: "string"
  },
  // ...(noType ? [] : [{ path: "type", type: "string" }]),
  sizeSchema, {
    path: "strand",
    displayName: "位于正/负链",
    type: "string"
  }]
};
var RemoveDuplicatesDialog = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(RemoveDuplicatesDialog, _React$Component);
  function RemoveDuplicatesDialog() {
    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), "state", {
      dups: []
    });
    _defineProperty(_assertThisInitialized(_this), "checkboxStyle", {
      marginTop: 0,
      marginBottom: 0
    });
    _defineProperty(_assertThisInitialized(_this), "delayedRecomputeDups", function () {
      setTimeout(function () {
        _this.recomputeDups();
      });
    });
    _defineProperty(_assertThisInitialized(_this), "recomputeDups", function () {
      var _this$props = _this.props,
        type = _this$props.type,
        _this$props$sequenceD = _this$props.sequenceData,
        sequenceData = _this$props$sequenceD === void 0 ? {
          sequence: ""
        } : _this$props$sequenceD,
        sequenceLength = _this$props.sequenceLength,
        ignoreName = _this$props.ignoreName,
        ignoreStrand = _this$props.ignoreStrand,
        ignoreStartAndEnd = _this$props.ignoreStartAndEnd;
      var annotations = sequenceData[type];
      var dups = [];
      var seqsHashByStartEndStrandName = {};
      forEach(annotations, function (a) {
        var hash = (ignoreStartAndEnd ? "" : a.start) + "&" + (ignoreStartAndEnd ? "" : a.end) + "&" + (ignoreStrand ? "" : a.strand) + "&" + (ignoreName ? "" : a.name);
        if (seqsHashByStartEndStrandName[hash]) {
          dups.push(_extends({}, a, {
            size: getRangeLength(a, sequenceLength)
          }));
        } else {
          seqsHashByStartEndStrandName[hash] = true;
        }
      });
      _this.setState({
        dups: dups
      });
    });
    return _this;
  }
  var _proto = RemoveDuplicatesDialog.prototype;
  _proto.componentDidMount = function componentDidMount() {
    this.recomputeDups();
  };
  _proto.render = function render() {
    var _this2 = this;
    var _this$props2 = this.props,
      duplicatesToRemoveSelectedEntities = _this$props2.duplicatesToRemoveSelectedEntities,
      hideModal = _this$props2.hideModal,
      type = _this$props2.type;
    var selectedIds = this.state.dups.map(function (d) {
      return d.id;
    });
    // const sequenceLength = sequenceData.sequence.length;
    // const isCirc = (this.state || {}).circular;
    return /*#__PURE__*/React.createElement("div", {
      className: classNames(Classes.DIALOG_BODY, "tg-min-width-dialog")
    }, /*#__PURE__*/React.createElement(DataTable, {
      noPadding: true,
      withCheckboxes: true,
      noFullscreenButton: true
      // onRowSelect={this.onRowSelect}
      ,
      maxHeight: 400,
      selectedIds: selectedIds,
      formName: "duplicatesToRemove",
      noRouter: true,
      noRowsFoundMessage: "No duplicates found",
      compact: true,
      noHeader: true,
      noFooter: true,
      withSearch: false,
      hideSelectedCount: true,
      isInfinite: true,
      schema: schema,
      entities: this.state.dups
    }), /*#__PURE__*/React.createElement("div", {
      style: {
        marginTop: 10,
        display: "flex",
        justifyContent: "space-between"
      }
    }, /*#__PURE__*/React.createElement(Popover, {
      target: /*#__PURE__*/React.createElement(Button, {
        icon: "settings"
      }),
      content: /*#__PURE__*/React.createElement("div", {
        style: {
          padding: 20,
          maxWidth: 250
        }
      }, /*#__PURE__*/React.createElement("div", null, "\u5728\u67E5\u627E\u91CD\u590D\u9879\u65F6\u5FFD\u7565\u8FD9\u4E9B\u5B57\u6BB5:"), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement(SwitchField, {
        containerStyle: {
          marginBottom: 2
        }
        //delay the call to recompute dups until redux has had time to update
        ,
        onFieldSubmit: this.delayedRecomputeDups,
        style: this.checkboxStyle,
        name: "ignoreName",
        label: "\u540D\u79F0"
      }), /*#__PURE__*/React.createElement(SwitchField, {
        containerStyle: {
          marginBottom: 2
        }
        //delay the call to recompute dups until redux has had time to update
        ,
        onFieldSubmit: this.delayedRecomputeDups,
        style: this.checkboxStyle,
        name: "ignoreStrand",
        label: "\u4F4D\u4E8E\u6B63/\u8D1F\u94FE"
      }), /*#__PURE__*/React.createElement(SwitchField, {
        containerStyle: {
          marginBottom: 2
        }
        //delay the call to recompute dups until redux has had time to update
        ,
        onFieldSubmit: this.delayedRecomputeDups,
        style: this.checkboxStyle,
        name: "ignoreStartAndEnd",
        label: "\u5F00\u59CB\u548C\u7ED3\u675F"
      }))
    }), /*#__PURE__*/React.createElement(Button, {
      intent: "primary",
      onClick: function onClick() {
        _this2.props[camelCase("delete_" + type).slice(0, -1)](duplicatesToRemoveSelectedEntities.map(function (d) {
          return d.id;
        }));
        window.toastr.success("Successfully Deleted " + duplicatesToRemoveSelectedEntities.length + " " + startCase(type));
        hideModal();
      },
      disabled: !(duplicatesToRemoveSelectedEntities || []).length
    }, "\u5220\u9664 ", duplicatesToRemoveSelectedEntities.length, " \u4E2A\u91CD\u590D\u7684")));
  };
  return RemoveDuplicatesDialog;
}(React.Component);
export default compose(wrapDialog(), withEditorProps, withSelectedEntities("duplicatesToRemove"), reduxForm({
  form: "RemoveDuplicatesDialog"
}), tgFormValues("ignoreName", "ignoreStrand", "ignoreStartAndEnd"))(RemoveDuplicatesDialog);