'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; // Components

// Mixins

// Helpers


var _VData = require('../VData');

var _VDataFooter = require('./VDataFooter');

var _VDataFooter2 = _interopRequireDefault(_VDataFooter);

var _mixins = require('../../util/mixins');

var _mixins2 = _interopRequireDefault(_mixins);

var _themeable = require('../../mixins/themeable');

var _themeable2 = _interopRequireDefault(_themeable);

var _helpers = require('../../util/helpers');

var _console = require('../../util/console');

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

/* @vue/component */
exports.default = (0, _mixins2.default)(_themeable2.default).extend({
    name: 'v-data-iterator',
    props: _extends({}, _VData.VData.options.props, {
        value: {
            type: Array,
            default: function _default() {
                return [];
            }
        },
        singleSelect: Boolean,
        expanded: {
            type: Array,
            default: function _default() {
                return [];
            }
        },
        singleExpand: Boolean,
        loading: [Boolean, String],
        noResultsText: {
            type: String,
            default: '$vuetify.dataIterator.noResultsText'
        },
        noDataText: {
            type: String,
            default: '$vuetify.noDataText'
        },
        loadingText: {
            type: String,
            default: '$vuetify.dataIterator.loadingText'
        },
        hideDefaultFooter: Boolean,
        footerProps: Object
    }),
    data: function data() {
        return {
            selection: {},
            expansion: {},
            internalCurrentItems: []
        };
    },
    computed: {
        everyItem: function everyItem() {
            var _this = this;

            return !!this.internalCurrentItems.length && this.internalCurrentItems.every(function (i) {
                return _this.isSelected(i);
            });
        },
        someItems: function someItems() {
            var _this2 = this;

            return this.internalCurrentItems.some(function (i) {
                return _this2.isSelected(i);
            });
        }
    },
    watch: {
        value: function value(_value) {
            var _this3 = this;

            this.selection = _value.reduce(function (selection, item) {
                selection[(0, _helpers.getObjectValueByPath)(item, _this3.itemKey)] = true;
                return selection;
            }, {});
        },

        selection: {
            handler: function handler(value, old) {
                var _this4 = this;

                if ((0, _helpers.deepEqual)(value, old)) return;
                var keys = Object.keys(value).filter(function (k) {
                    return value[k];
                });
                var selected = !keys.length ? [] : this.items.filter(function (i) {
                    return keys.includes(String((0, _helpers.getObjectValueByPath)(i, _this4.itemKey)));
                });
                this.$emit('input', selected);
            },

            deep: true
        },
        expanded: function expanded(value) {
            var _this5 = this;

            this.expansion = value.reduce(function (expansion, item) {
                expansion[(0, _helpers.getObjectValueByPath)(item, _this5.itemKey)] = true;
                return expansion;
            }, {});
        },
        expansion: function expansion(value, old) {
            var _this6 = this;

            if ((0, _helpers.deepEqual)(value, old)) return;
            var keys = Object.keys(value).filter(function (k) {
                return value[k];
            });
            var expanded = !keys.length ? [] : this.items.filter(function (i) {
                return keys.includes(String((0, _helpers.getObjectValueByPath)(i, _this6.itemKey)));
            });
            this.$emit('update:expanded', expanded);
        }
    },
    created: function created() {
        var _this7 = this;

        var breakingProps = [['disable-initial-sort', 'sort-by'], ['filter', 'custom-filter'], ['pagination', 'options'], ['total-items', 'server-items-length'], ['hide-actions', 'hide-default-footer'], ['rows-per-page-items', 'footer-props.items-per-page-options'], ['rows-per-page-text', 'footer-props.items-per-page-text'], ['prev-icon', 'footer-props.prev-icon'], ['next-icon', 'footer-props.next-icon']];
        breakingProps.forEach(function (_ref) {
            var _ref2 = _slicedToArray(_ref, 2),
                original = _ref2[0],
                replacement = _ref2[1];

            if (_this7.$attrs.hasOwnProperty(original)) (0, _console.breaking)(original, replacement);
        });
        var removedProps = ['expand', 'content-class', 'content-props', 'content-tag'];
        removedProps.forEach(function (prop) {
            if (_this7.$attrs.hasOwnProperty(prop)) (0, _console.removed)(prop);
        });
    },

    methods: {
        toggleSelectAll: function toggleSelectAll(value) {
            var _this8 = this;

            this.internalCurrentItems.forEach(function (item) {
                var key = (0, _helpers.getObjectValueByPath)(item, _this8.itemKey);
                _this8.$set(_this8.selection, key, value);
            });
        },
        isSelected: function isSelected(item) {
            return this.selection[(0, _helpers.getObjectValueByPath)(item, this.itemKey)] || false;
        },
        select: function select(item) {
            var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

            var selection = this.singleSelect ? {} : Object.assign({}, this.selection);
            var key = (0, _helpers.getObjectValueByPath)(item, this.itemKey);
            if (value) selection[key] = true;else delete selection[key];
            this.selection = selection;
            this.$emit('item-selected', { item: item, value: value });
        },
        isExpanded: function isExpanded(item) {
            return this.expansion[(0, _helpers.getObjectValueByPath)(item, this.itemKey)] || false;
        },
        expand: function expand(item) {
            var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

            var expansion = this.singleExpand ? {} : Object.assign({}, this.expansion);
            var key = (0, _helpers.getObjectValueByPath)(item, this.itemKey);
            if (value) expansion[key] = true;else delete expansion[key];
            this.expansion = expansion;
            this.$emit('item-expanded', { item: item, value: value });
        },
        createItemProps: function createItemProps(item) {
            var _this9 = this;

            var props = {
                item: item,
                select: {
                    props: {
                        value: this.isSelected(item)
                    },
                    on: {
                        input: function input(v) {
                            return _this9.select(item, v);
                        }
                    }
                },
                expand: {
                    props: {
                        value: this.isExpanded(item)
                    },
                    on: {
                        input: function input(v) {
                            return _this9.expand(item, v);
                        }
                    }
                }
            };
            return props;
        },
        genEmptyWrapper: function genEmptyWrapper(content) {
            return this.$createElement('div', content);
        },
        genEmpty: function genEmpty(itemsLength) {
            if (itemsLength <= 0 && this.loading) {
                var loading = this.$slots['loading'] || this.$vuetify.lang.t(this.loadingText);
                return this.genEmptyWrapper(loading);
            } else if (itemsLength <= 0 && !this.items.length) {
                var noData = this.$slots['no-data'] || this.$vuetify.lang.t(this.noDataText);
                return this.genEmptyWrapper(noData);
            } else if (itemsLength <= 0 && this.search) {
                var noResults = this.$slots['no-results'] || this.$vuetify.lang.t(this.noResultsText);
                return this.genEmptyWrapper(noResults);
            }
            return null;
        },
        genItems: function genItems(props) {
            var _this10 = this;

            var empty = this.genEmpty(props.pagination.itemsLength);
            if (empty) return [empty];
            if (this.$scopedSlots.default) {
                return this.$scopedSlots.default(_extends({}, props, {
                    isSelected: this.isSelected,
                    select: this.select,
                    isExpanded: this.isExpanded,
                    expand: this.expand
                }));
            }
            if (this.$scopedSlots.item) {
                return props.items.map(function (item) {
                    return _this10.$scopedSlots.item(_this10.createItemProps(item));
                });
            }
            return [];
        },
        genFooter: function genFooter(props) {
            if (this.hideDefaultFooter) return null;
            var data = {
                props: _extends({}, this.footerProps, {
                    options: props.options,
                    pagination: props.pagination
                }),
                on: {
                    'update:options': function updateOptions(value) {
                        return props.updateOptions(value);
                    }
                }
            };
            var scopedSlots = (0, _helpers.getPrefixedScopedSlots)('footer.', this.$scopedSlots);
            return this.$createElement(_VDataFooter2.default, _extends({
                scopedSlots: scopedSlots
            }, data));
        },
        genSlots: function genSlots(slot) {
            var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

            if (this.$scopedSlots[slot]) return this.$scopedSlots[slot](props);else if (this.$slots[slot]) return this.$slots[slot];
            return [];
        },
        genDefaultScopedSlot: function genDefaultScopedSlot(props) {
            return this.$createElement('div', {
                staticClass: 'v-data-iterator'
            }, [this.genSlots('header', props), this.genItems(props), this.genFooter(props), this.genSlots('footer', props)]);
        }
    },
    render: function render() {
        var _this11 = this;

        return this.$createElement(_VData.VData, {
            props: this.$props,
            on: {
                'update:options': function updateOptions(v, old) {
                    return !(0, _helpers.deepEqual)(v, old) && _this11.$emit('update:options', v);
                },
                'update:page': function updatePage(v) {
                    return _this11.$emit('update:page', v);
                },
                'update:items-per-page': function updateItemsPerPage(v) {
                    return _this11.$emit('update:items-per-page', v);
                },
                'update:sort-by': function updateSortBy(v) {
                    return _this11.$emit('update:sort-by', v);
                },
                'update:sort-desc': function updateSortDesc(v) {
                    return _this11.$emit('update:sort-desc', v);
                },
                'update:group-by': function updateGroupBy(v) {
                    return _this11.$emit('update:group-by', v);
                },
                'update:group-desc': function updateGroupDesc(v) {
                    return _this11.$emit('update:group-desc', v);
                },
                'pagination': function pagination(v, old) {
                    return !(0, _helpers.deepEqual)(v, old) && _this11.$emit('pagination', v);
                },
                'current-items': function currentItems(v) {
                    _this11.internalCurrentItems = v;
                    _this11.$emit('current-items', v);
                }
            },
            scopedSlots: {
                default: this.genDefaultScopedSlot
            }
        });
    }
});
//# sourceMappingURL=VDataIterator.js.map