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 { EVENT_NAME_CONTEXT_CHANGED } from '../../../constants/events';
import { PROP_TYPE_ARRAY, PROP_TYPE_STRING } from '../../../constants/props';
import { useParentMixin } from '../../../mixins/use-parent';
import { isArray, isFunction, isString } from '../../../utils/inspect';
import { looseEqual } from '../../../utils/loose-equal';
import { mathMax } from '../../../utils/math';
import { makeModelMixin } from '../../../utils/model';
import { toInteger } from '../../../utils/number';
import { clone, sortKeys } from '../../../utils/object';
import { makeProp } from '../../../utils/props';
import { safeVueInstance } from '../../../utils/safe-vue-instance';
import { normalizeFields } from './normalize-fields';

// --- Constants ---

var _makeModelMixin = makeModelMixin('value', {
    type: PROP_TYPE_ARRAY,
    defaultValue: []
  }),
  modelMixin = _makeModelMixin.mixin,
  modelProps = _makeModelMixin.props,
  MODEL_PROP_NAME = _makeModelMixin.prop,
  MODEL_EVENT_NAME = _makeModelMixin.event;
export { MODEL_PROP_NAME, MODEL_EVENT_NAME };

// --- Props ---

export var props = sortKeys(_objectSpread(_objectSpread({}, modelProps), {}, _defineProperty({
  fields: makeProp(PROP_TYPE_ARRAY, null),
  // Provider mixin adds in `Function` type
  items: makeProp(PROP_TYPE_ARRAY, []),
  // Primary key for record
  // If provided the value in each row must be unique!
  primaryKey: makeProp(PROP_TYPE_STRING)
}, MODEL_PROP_NAME, makeProp(PROP_TYPE_ARRAY, []))));

// --- Mixin ---

// @vue/component
export var itemsMixin = extend({
  mixins: [modelMixin, useParentMixin],
  props: props,
  data: function data() {
    var items = this.items;
    return {
      // Our local copy of the items
      // Must be an array
      localItems: isArray(items) ? items.slice() : []
    };
  },
  computed: {
    computedFields: function computedFields() {
      // We normalize fields into an array of objects
      // `[ { key:..., label:..., ...}, {...}, ..., {..}]`
      return normalizeFields(this.fields, this.localItems);
    },
    computedFieldsObj: function computedFieldsObj() {
      // Fields as a simple lookup hash object
      // Mainly for formatter lookup and use in `scopedSlots` for convenience
      // If the field has a formatter, it normalizes formatter to a
      // function ref or `undefined` if no formatter
      var bvParent = this.bvParent;
      return this.computedFields.reduce(function (obj, f) {
        // We use object spread here so we don't mutate the original field object
        obj[f.key] = clone(f);
        if (f.formatter) {
          // Normalize formatter to a function ref or `undefined`
          var formatter = f.formatter;
          if (isString(formatter) && isFunction(bvParent[formatter])) {
            formatter = bvParent[formatter];
          } else if (!isFunction(formatter)) {
            /* istanbul ignore next */
            formatter = undefined;
          }
          // Return formatter function or `undefined` if none
          obj[f.key].formatter = formatter;
        }
        return obj;
      }, {});
    },
    computedItems: function computedItems() {
      var _safeVueInstance = safeVueInstance(this),
        paginatedItems = _safeVueInstance.paginatedItems,
        sortedItems = _safeVueInstance.sortedItems,
        filteredItems = _safeVueInstance.filteredItems,
        localItems = _safeVueInstance.localItems;
      // Fallback if various mixins not provided
      return (paginatedItems || sortedItems || filteredItems || localItems || /* istanbul ignore next */
      []).slice();
    },
    context: function context() {
      var _safeVueInstance2 = safeVueInstance(this),
        perPage = _safeVueInstance2.perPage,
        currentPage = _safeVueInstance2.currentPage;
      // Current state of sorting, filtering and pagination props/values
      return {
        filter: this.localFilter,
        sortBy: this.localSortBy,
        sortDesc: this.localSortDesc,
        perPage: mathMax(toInteger(perPage, 0), 0),
        currentPage: mathMax(toInteger(currentPage, 0), 1),
        apiUrl: this.apiUrl
      };
    }
  },
  watch: {
    items: function items(newValue) {
      // Set `localItems`/`filteredItems` to a copy of the provided array
      this.localItems = isArray(newValue) ? newValue.slice() : [];
    },
    // Watch for changes on `computedItems` and update the `v-model`
    computedItems: function computedItems(newValue, oldValue) {
      if (!looseEqual(newValue, oldValue)) {
        this.$emit(MODEL_EVENT_NAME, newValue);
      }
    },
    // Watch for context changes
    context: function context(newValue, oldValue) {
      // Emit context information for external paging/filtering/sorting handling
      if (!looseEqual(newValue, oldValue)) {
        this.$emit(EVENT_NAME_CONTEXT_CHANGED, newValue);
      }
    }
  },
  mounted: function mounted() {
    // Initially update the `v-model` of displayed items
    this.$emit(MODEL_EVENT_NAME, this.computedItems);
  },
  methods: {
    // Method to get the formatter method for a given field key
    getFieldFormatter: function getFieldFormatter(key) {
      var field = this.computedFieldsObj[key];
      // `this.computedFieldsObj` has pre-normalized the formatter to a
      // function ref if present, otherwise `undefined`
      return field ? field.formatter : undefined;
    }
  }
});