function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { extend } from '../../vue';
import { NAME_FORM_CHECKBOX } from '../../constants/components';
import { EVENT_NAME_CHANGE, MODEL_EVENT_NAME_PREFIX } from '../../constants/events';
import { PROP_TYPE_ANY, PROP_TYPE_BOOLEAN } from '../../constants/props';
import { isArray } from '../../utils/inspect';
import { looseEqual } from '../../utils/loose-equal';
import { looseIndexOf } from '../../utils/loose-index-of';
import { sortKeys } from '../../utils/object';
import { makeProp, makePropsConfigurable } from '../../utils/props';
import { MODEL_EVENT_NAME, formRadioCheckMixin, props as formRadioCheckProps } from '../../mixins/form-radio-check';

// --- Constants ---

var MODEL_PROP_NAME_INDETERMINATE = 'indeterminate';
var MODEL_EVENT_NAME_INDETERMINATE = MODEL_EVENT_NAME_PREFIX + MODEL_PROP_NAME_INDETERMINATE;

// --- Props ---

export var props = makePropsConfigurable(sortKeys(_objectSpread(_objectSpread({}, formRadioCheckProps), {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, MODEL_PROP_NAME_INDETERMINATE, makeProp(PROP_TYPE_BOOLEAN, false)), "switch", makeProp(PROP_TYPE_BOOLEAN, false)), "uncheckedValue", makeProp(PROP_TYPE_ANY, false)), "value", makeProp(PROP_TYPE_ANY, true)))), NAME_FORM_CHECKBOX);

// --- Main component ---

// @vue/component
export var BFormCheckbox = /*#__PURE__*/extend({
  name: NAME_FORM_CHECKBOX,
  mixins: [formRadioCheckMixin],
  inject: {
    getBvGroup: {
      from: 'getBvCheckGroup',
      default: function _default() {
        return function () {
          return null;
        };
      }
    }
  },
  props: props,
  computed: {
    bvGroup: function bvGroup() {
      return this.getBvGroup();
    },
    isChecked: function isChecked() {
      var value = this.value,
        checked = this.computedLocalChecked;
      return isArray(checked) ? looseIndexOf(checked, value) > -1 : looseEqual(checked, value);
    },
    isRadio: function isRadio() {
      return false;
    }
  },
  watch: _defineProperty({}, MODEL_PROP_NAME_INDETERMINATE, function (newValue, oldValue) {
    if (!looseEqual(newValue, oldValue)) {
      this.setIndeterminate(newValue);
    }
  }),
  mounted: function mounted() {
    // Set initial indeterminate state
    this.setIndeterminate(this[MODEL_PROP_NAME_INDETERMINATE]);
  },
  methods: {
    computedLocalCheckedWatcher: function computedLocalCheckedWatcher(newValue, oldValue) {
      if (!looseEqual(newValue, oldValue)) {
        this.$emit(MODEL_EVENT_NAME, newValue);
        var $input = this.$refs.input;
        if ($input) {
          this.$emit(MODEL_EVENT_NAME_INDETERMINATE, $input.indeterminate);
        }
      }
    },
    handleChange: function handleChange(_ref) {
      var _this = this;
      var _ref$target = _ref.target,
        checked = _ref$target.checked,
        indeterminate = _ref$target.indeterminate;
      var value = this.value,
        uncheckedValue = this.uncheckedValue;

      // Update `computedLocalChecked`
      var localChecked = this.computedLocalChecked;
      if (isArray(localChecked)) {
        var index = looseIndexOf(localChecked, value);
        if (checked && index < 0) {
          // Add value to array
          localChecked = localChecked.concat(value);
        } else if (!checked && index > -1) {
          // Remove value from array
          localChecked = localChecked.slice(0, index).concat(localChecked.slice(index + 1));
        }
      } else {
        localChecked = checked ? value : uncheckedValue;
      }
      this.computedLocalChecked = localChecked;

      // Fire events in a `$nextTick()` to ensure the `v-model` is updated
      this.$nextTick(function () {
        // Change is only emitted on user interaction
        _this.$emit(EVENT_NAME_CHANGE, localChecked);

        // If this is a child of a group, we emit a change event on it as well
        if (_this.isGroup) {
          _this.bvGroup.$emit(EVENT_NAME_CHANGE, localChecked);
        }
        _this.$emit(MODEL_EVENT_NAME_INDETERMINATE, indeterminate);
      });
    },
    setIndeterminate: function setIndeterminate(state) {
      // Indeterminate only supported in single checkbox mode
      if (isArray(this.computedLocalChecked)) {
        state = false;
      }
      var $input = this.$refs.input;
      if ($input) {
        $input.indeterminate = state;
        // Emit update event to prop
        this.$emit(MODEL_EVENT_NAME_INDETERMINATE, state);
      }
    }
  }
});