import "core-js/modules/es6.number.constructor";
import _createClass from "@babel/runtime-corejs2/helpers/createClass";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import Intact from 'intact-angular';
import template from './column.vdt';

var TableColumn =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(TableColumn, _Intact);

  function TableColumn() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = TableColumn.prototype;

  _proto.defaults = function defaults() {
    return {
      title: undefined,
      template: function template(data, index) {},
      key: '',
      sortable: false,
      width: undefined,
      group: undefined,
      multiple: false,
      value: [],
      minWidth: undefined,
      ignore: false,
      fixed: undefined,
      align: undefined,
      rows: undefined,
      cols: undefined,
      invisible: false,
      // for omiting it by getRestProps
      blockFn: undefined,
      // passed by parent
      $parent: undefined,
      _show: false
    };
  };

  _proto._init = function _init() {
    var _this = this;

    this.on('$receive:width', function (c, v) {
      _this.width = v;
    });
  };

  _proto.onClick = function onClick(e) {
    this.trigger('click', e);
  };

  _proto.onDragStart = function onDragStart(e) {
    this.trigger('dragStart', this.vNode, e);
  };

  _proto._isChecked = function _isChecked(v) {
    var _this$get = this.get(),
        value = _this$get.value,
        multiple = _this$get.multiple;

    if (multiple) {
      return !!(value && ~value.indexOf(v));
    }

    return value === v;
  };

  _proto._onSelect = function _onSelect(v, c, e) {
    e.preventDefault();

    var _this$get2 = this.get(),
        value = _this$get2.value,
        multiple = _this$get2.multiple;

    if (multiple) {
      var tmp = value ? value.slice(0) : [];
      var index = tmp.indexOf(v);

      if (~index) {
        tmp.splice(index, 1);
      } else {
        tmp.push(v);
      }

      this.set('value', tmp);
    } else {
      this.set('value', v);
    }
  };

  _proto._onChangeShow = function _onChangeShow(c, v) {
    this.set('_show', v);
  };

  _proto._getGroupText = function _getGroupText() {
    var _this$get3 = this.get(),
        value = _this$get3.value,
        multiple = _this$get3.multiple,
        group = _this$get3.group;

    if (value == null) return;

    if (!multiple) {
      value = [value];
    }

    var ret;

    if (group) {
      ret = group.filter(function (item) {
        return ~value.indexOf(item.value);
      }).map(function (item) {
        return item.label;
      }).join(', ');

      if (ret) {
        return "(" + ret + ")";
      }
    }
  };

  _proto._stopPropagation = function _stopPropagation(e) {
    e.stopPropagation();
  };

  _createClass(TableColumn, [{
    key: "template",
    get: function get() {
      return template;
    }
  }]);

  return TableColumn;
}(Intact);

_defineProperty(TableColumn, "blocks", ['title', 'template', 'default']);

_defineProperty(TableColumn, "propTypes", {
  title: [String, Intact.VNode, Array, Object
  /* for vue vnode*/
  ],
  template: Function,
  key: {
    type: String,
    required: true
  },
  sortable: Boolean,
  width: [Number, String],
  group: Array,
  multiple: Boolean,
  minWidth: [Number, String],
  ignore: Boolean,
  fixed: ['left', 'right'],
  align: ['left', 'center', 'right'],
  rows: Number,
  cols: Number,
  invisible: Boolean
});

_defineProperty(TableColumn, "events", {
  click: true,
  dragStart: true
});

export { TableColumn as default };