var _a;
import { __decorate, __extends } from "tslib";
import Vue from 'vue';
import Component from 'vue-class-component';
import { Emit, Prop, Watch } from 'vue-property-decorator';
import { Enums } from '../../utils/enums/enums';
import AccordionPlugin, { MAccordionSkin } from '../accordion/accordion';
import { ACCORDION_GROUP_NAME, I18N_NAME, LINK_NAME } from '../component-names';
import { MI18n } from '../i18n/i18n';
import { MLink, MLinkMode } from '../link/link';
import WithRender from './accordion-group.html?style=./accordion-group.scss';
var MAccordionGroup = /** @class */ (function (_super) {
    __extends(MAccordionGroup, _super);
    function MAccordionGroup() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.isAllOpen = false;
        _this.isAllClosed = false;
        _this.linkModeButton = MLinkMode.Button;
        _this.accordions = {};
        return _this;
    }
    MAccordionGroup.prototype.emitUpdateOpenedIds = function (openedIds) { };
    MAccordionGroup.prototype.onOpenedIdsChange = function (openedIds) {
        var _loop_1 = function (id) {
            this_1.accordions[id].propOpen =
                openedIds.find(function (openedId) { return openedId === id; }) !== undefined;
        };
        var this_1 = this;
        for (var id in this.accordions) {
            _loop_1(id);
        }
    };
    Object.defineProperty(MAccordionGroup.prototype, "propSkin", {
        get: function () {
            return this.skin || MAccordionSkin.Default;
        },
        enumerable: false,
        configurable: true
    });
    MAccordionGroup.prototype.addAccordion = function (accordion) {
        accordion.$on('update:open', this.onAccordionOpenChange);
        this.$set(this.accordions, accordion.propId, accordion);
        if (this.openAll || (this.openedIds && this.openedIds.find(function (v) { return v === accordion.propId; }))) {
            accordion.propOpen = true;
        }
    };
    MAccordionGroup.prototype.removeAccordion = function (id) {
        this.accordions[id].$off('update:open', this.onAccordionOpenChange);
        this.$delete(this.accordions, id);
    };
    MAccordionGroup.prototype.closeAllAccordions = function () {
        for (var id in this.accordions) {
            if (!this.accordions[id].propDisabled) {
                this.accordions[id].propOpen = false;
            }
        }
    };
    MAccordionGroup.prototype.hasTitleSlot = function () {
        return !!this.$slots['title'] || !!this.$scopedSlots['title'];
    };
    MAccordionGroup.prototype.hasSecondaryContentSlot = function () {
        return Boolean(this.$slots['secondary-content']) || Boolean(this.$scopedSlots['secondary-content']);
    };
    MAccordionGroup.prototype.openAllAccordions = function () {
        for (var id in this.accordions) {
            if (!this.accordions[id].propDisabled) {
                this.accordions[id].propOpen = true;
            }
        }
    };
    MAccordionGroup.prototype.onAccordionOpenChange = function () {
        var _this = this;
        this.updateToggleLinks();
        var openedIds = Object.keys(this.accordions).filter(function (id) { return _this.accordions[id].propOpen; });
        this.emitUpdateOpenedIds(openedIds);
    };
    MAccordionGroup.prototype.updateToggleLinks = function () {
        this.updateIsAllOpen();
        this.updateIsAllClosed();
    };
    MAccordionGroup.prototype.mounted = function () {
        this.updateToggleLinks();
    };
    MAccordionGroup.prototype.updated = function () {
        this.updateToggleLinks();
    };
    MAccordionGroup.prototype.updateIsAllOpen = function () {
        // A computed wouldn't work here since we want to recompute the value each time the DOM changes (when a child is removed, added, updated, etc)
        var allOpened = true;
        for (var id in this.accordions) {
            allOpened = this.accordions[id].propOpen || !this.accordionHasContent(id);
            if (!allOpened && !this.accordions[id].propDisabled) {
                break;
            }
        }
        this.isAllOpen = allOpened;
    };
    MAccordionGroup.prototype.updateIsAllClosed = function () {
        // A computed wouldn't work here since we want to recompute the value each time the DOM changes (when a child is removed, added, updated, etc)
        var allClosed = true;
        for (var id in this.accordions) {
            allClosed = !this.accordions[id].propOpen || !this.accordionHasContent(id);
            if (!allClosed && !this.accordions[id].propDisabled) {
                break;
            }
        }
        this.isAllClosed = allClosed;
    };
    MAccordionGroup.prototype.accordionHasContent = function (id) {
        return !!this.accordions[id].$slots.default ||
            (!!this.accordions[id].$scopedSlots.default && !!this.accordions[id].$scopedSlots.default(undefined));
    };
    __decorate([
        Prop({
            default: MAccordionSkin.Default,
            validator: function (value) { return Enums.toValueArray(MAccordionSkin).includes(value); }
        })
    ], MAccordionGroup.prototype, "skin", void 0);
    __decorate([
        Prop({
            default: false
        })
    ], MAccordionGroup.prototype, "concurrent", void 0);
    __decorate([
        Prop({
            default: false
        })
    ], MAccordionGroup.prototype, "disabled", void 0);
    __decorate([
        Prop()
    ], MAccordionGroup.prototype, "openedIds", void 0);
    __decorate([
        Prop({
            default: false
        })
    ], MAccordionGroup.prototype, "toggleLinkLeft", void 0);
    __decorate([
        Prop({
            default: false
        })
    ], MAccordionGroup.prototype, "openAll", void 0);
    __decorate([
        Emit('update:openedIds')
    ], MAccordionGroup.prototype, "emitUpdateOpenedIds", null);
    __decorate([
        Watch('openedIds')
    ], MAccordionGroup.prototype, "onOpenedIdsChange", null);
    MAccordionGroup = __decorate([
        WithRender,
        Component({
            components: (_a = {},
                _a[LINK_NAME] = MLink,
                _a[I18N_NAME] = MI18n,
                _a)
        })
    ], MAccordionGroup);
    return MAccordionGroup;
}(Vue));
export { MAccordionGroup };
var AccordionGroupPlugin = {
    install: function (v) {
        v.use(AccordionPlugin);
        v.component(ACCORDION_GROUP_NAME, MAccordionGroup);
    }
};
export default AccordionGroupPlugin;
//# sourceMappingURL=accordion-group.js.map