"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _DownOutlined = _interopRequireDefault(require("@ant-design/icons-vue/DownOutlined"));

var _checkbox = _interopRequireDefault(require("../checkbox"));

var _dropdown = _interopRequireDefault(require("../dropdown"));

var _menu = _interopRequireDefault(require("../menu"));

var _classNames2 = _interopRequireDefault(require("../_util/classNames"));

var _interface = require("./interface");

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function checkSelection(_ref) {
  var store = _ref.store,
      getCheckboxPropsByItem = _ref.getCheckboxPropsByItem,
      getRecordKey = _ref.getRecordKey,
      data = _ref.data,
      type = _ref.type,
      byDefaultChecked = _ref.byDefaultChecked;
  return byDefaultChecked ? data[type](function (item, i) {
    return getCheckboxPropsByItem(item, i).defaultChecked;
  }) : data[type](function (item, i) {
    return store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0;
  });
}

function getIndeterminateState(props) {
  var store = props.store,
      data = props.data;

  if (!data.length) {
    return false;
  }

  var someCheckedNotByDefaultChecked = checkSelection(_objectSpread(_objectSpread({}, props), {}, {
    data: data,
    type: 'some',
    byDefaultChecked: false
  })) && !checkSelection(_objectSpread(_objectSpread({}, props), {}, {
    data: data,
    type: 'every',
    byDefaultChecked: false
  }));
  var someCheckedByDefaultChecked = checkSelection(_objectSpread(_objectSpread({}, props), {}, {
    data: data,
    type: 'some',
    byDefaultChecked: true
  })) && !checkSelection(_objectSpread(_objectSpread({}, props), {}, {
    data: data,
    type: 'every',
    byDefaultChecked: true
  }));

  if (store.getState().selectionDirty) {
    return someCheckedNotByDefaultChecked;
  }

  return someCheckedNotByDefaultChecked || someCheckedByDefaultChecked;
}

function getCheckState(props) {
  var store = props.store,
      data = props.data;

  if (!data.length) {
    return false;
  }

  if (store.getState().selectionDirty) {
    return checkSelection(_objectSpread(_objectSpread({}, props), {}, {
      data: data,
      type: 'every',
      byDefaultChecked: false
    }));
  }

  return checkSelection(_objectSpread(_objectSpread({}, props), {}, {
    data: data,
    type: 'every',
    byDefaultChecked: false
  })) || checkSelection(_objectSpread(_objectSpread({}, props), {}, {
    data: data,
    type: 'every',
    byDefaultChecked: true
  }));
}

var _default2 = {
  name: 'SelectionCheckboxAll',
  mixins: [_BaseMixin.default],
  inheritAttrs: false,
  props: _interface.SelectionCheckboxAllProps,
  data: function data() {
    var props = this.$props;
    this.defaultSelections = props.hideDefaultSelections ? [] : [{
      key: 'all',
      text: props.locale.selectAll
    }, {
      key: 'invert',
      text: props.locale.selectInvert
    }];
    return {
      checked: getCheckState(props),
      indeterminate: getIndeterminateState(props)
    };
  },
  watch: {
    $props: {
      handler: function handler() {
        this.setCheckState(this.$props);
      },
      deep: true
    }
  },
  mounted: function mounted() {
    this.subscribe();
  },
  beforeUnmount: function beforeUnmount() {
    if (this.unsubscribe) {
      this.unsubscribe();
    }
  },
  methods: {
    checkSelection: function checkSelection(props, data, type, byDefaultChecked) {
      var _ref2 = props || this.$props,
          store = _ref2.store,
          getCheckboxPropsByItem = _ref2.getCheckboxPropsByItem,
          getRecordKey = _ref2.getRecordKey; // type should be 'every' | 'some'


      if (type === 'every' || type === 'some') {
        return byDefaultChecked ? data[type](function (item, i) {
          return getCheckboxPropsByItem(item, i).defaultChecked;
        }) : data[type](function (item, i) {
          return store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0;
        });
      }

      return false;
    },
    setCheckState: function setCheckState(props) {
      var checked = getCheckState(props);
      var indeterminate = getIndeterminateState(props);
      this.setState(function (prevState) {
        var newState = {};

        if (indeterminate !== prevState.indeterminate) {
          newState.indeterminate = indeterminate;
        }

        if (checked !== prevState.checked) {
          newState.checked = checked;
        }

        return newState;
      });
    },
    handleSelectAllChange: function handleSelectAllChange(e) {
      var checked = e.target.checked;
      this.$emit('select', checked ? 'all' : 'removeAll', 0, null);
    },
    subscribe: function subscribe() {
      var _this = this;

      var store = this.store;
      this.unsubscribe = store.subscribe(function () {
        _this.setCheckState(_this.$props);
      });
    },
    renderMenus: function renderMenus(selections) {
      var _this2 = this;

      return selections.map(function (selection, index) {
        return _vue.createVNode(_menu.default.Item, {
          "key": selection.key || index
        }, {
          default: function _default() {
            return [_vue.createVNode("div", {
              "onClick": function onClick() {
                _this2.$emit('select', selection.key, index, selection.onSelect);
              }
            }, [selection.text])];
          }
        });
      });
    }
  },
  render: function render() {
    var _this3 = this;

    var disabled = this.disabled,
        prefixCls = this.prefixCls,
        selections = this.selections,
        getPopupContainer = this.getPopupContainer,
        checked = this.checked,
        indeterminate = this.indeterminate;
    var selectionPrefixCls = "".concat(prefixCls, "-selection");
    var customSelections = null;

    if (selections) {
      var newSelections = Array.isArray(selections) ? this.defaultSelections.concat(selections) : this.defaultSelections;

      var menu = _vue.createVNode(_menu.default, {
        "class": "".concat(selectionPrefixCls, "-menu"),
        "selectedKeys": []
      }, {
        default: function _default() {
          return [_this3.renderMenus(newSelections)];
        }
      });

      customSelections = newSelections.length > 0 ? _vue.createVNode(_dropdown.default, {
        "getPopupContainer": getPopupContainer,
        "overlay": menu
      }, {
        default: function _default() {
          return [_vue.createVNode("div", {
            "class": "".concat(selectionPrefixCls, "-down")
          }, [_vue.createVNode(_DownOutlined.default, null, null)])];
        }
      }) : null;
    }

    return _vue.createVNode("div", {
      "class": selectionPrefixCls
    }, [_vue.createVNode(_checkbox.default, {
      "class": (0, _classNames2.default)(_defineProperty({}, "".concat(selectionPrefixCls, "-select-all-custom"), customSelections)),
      "checked": checked,
      "indeterminate": indeterminate,
      "disabled": disabled,
      "onChange": this.handleSelectAllChange
    }, null), customSelections]);
  }
};
exports.default = _default2;