import * as _vue from "vue";

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; }

import DownOutlined from '@ant-design/icons-vue/DownOutlined';
import Checkbox from '../checkbox';
import Dropdown from '../dropdown';
import Menu from '../menu';
import classNames from '../_util/classNames';
import { SelectionCheckboxAllProps } from './interface';
import BaseMixin from '../_util/BaseMixin';

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
  }));
}

export default {
  name: 'SelectionCheckboxAll',
  mixins: [BaseMixin],
  inheritAttrs: false,
  props: 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.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, {
        "class": "".concat(selectionPrefixCls, "-menu"),
        "selectedKeys": []
      }, {
        default: function _default() {
          return [_this3.renderMenus(newSelections)];
        }
      });

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

    return _vue.createVNode("div", {
      "class": selectionPrefixCls
    }, [_vue.createVNode(Checkbox, {
      "class": classNames(_defineProperty({}, "".concat(selectionPrefixCls, "-select-all-custom"), customSelections)),
      "checked": checked,
      "indeterminate": indeterminate,
      "disabled": disabled,
      "onChange": this.handleSelectAllChange
    }, null), customSelections]);
  }
};