/**
 * @dyna-grid/core - Advanced Data Grid / Data Table supporting Javascript / Web Components
 * @version v1.1.0
 * @link 
 * @license 
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Component } from './component';
import { RefSelector } from './componentAnnotations';
import { PostConstruct } from '../context/context';
import { createIcon } from '../utils/icon';
import { setDisplayed, removeCssClass, addCssClass, addOrRemoveCssClass } from '../utils/dom';
import { KeyCode } from '../constants/keyCode';
var DyGroupComponent = /** @class */ (function (_super) {
    __extends(DyGroupComponent, _super);
    function DyGroupComponent(params) {
        if (params === void 0) { params = {}; }
        var _this = _super.call(this, DyGroupComponent.getTemplate(params)) || this;
        _this.suppressEnabledCheckbox = true;
        _this.suppressOpenCloseIcons = false;
        var title = params.title, enabled = params.enabled, items = params.items, suppressEnabledCheckbox = params.suppressEnabledCheckbox, suppressOpenCloseIcons = params.suppressOpenCloseIcons;
        _this.title = title;
        _this.cssIdentifier = params.cssIdentifier || 'default';
        _this.enabled = enabled != null ? enabled : true;
        _this.items = items || [];
        _this.alignItems = params.alignItems || 'center';
        if (suppressEnabledCheckbox != null) {
            _this.suppressEnabledCheckbox = suppressEnabledCheckbox;
        }
        if (suppressOpenCloseIcons != null) {
            _this.suppressOpenCloseIcons = suppressOpenCloseIcons;
        }
        return _this;
    }
    DyGroupComponent.getTemplate = function (params) {
        var cssIdentifier = params.cssIdentifier || 'default';
        var direction = params.direction || 'vertical';
        return /* html */ "<div class=\"dy-group dy-" + cssIdentifier + "-group\">\n            <div class=\"dy-group-title-bar dy-" + cssIdentifier + "-group-title-bar dy-unselectable\" ref=\"eTitleBar\">\n                <span class=\"dy-group-title-bar-icon dy-" + cssIdentifier + "-group-title-bar-icon\" ref=\"eGroupOpenedIcon\"></span>\n                <span class=\"dy-group-title-bar-icon dy-" + cssIdentifier + "-group-title-bar-icon\" ref=\"eGroupClosedIcon\"></span>\n                <span ref=\"eTitle\" class=\"dy-group-title dy-" + cssIdentifier + "-group-title\"></span>\n            </div>\n            <div ref=\"eToolbar\" class=\"dy-group-toolbar dy-" + cssIdentifier + "-group-toolbar\">\n                <dy-checkbox ref=\"cbGroupEnabled\"></dy-checkbox>\n            </div>\n            <div ref=\"eContainer\" class=\"dy-group-container dy-group-container-" + direction + " dy-" + cssIdentifier + "-group-container\"></div>\n        </div>";
    };
    DyGroupComponent.prototype.postConstruct = function () {
        if (this.items.length) {
            var initialItems = this.items;
            this.items = [];
            this.addItems(initialItems);
        }
        var localeTextFunc = this.gridOptionsWrapper.getLocaleTextFunc();
        this.cbGroupEnabled.setLabel(localeTextFunc('enabled', 'Enabled'));
        if (this.title) {
            this.setTitle(this.title);
        }
        if (this.enabled) {
            this.setEnabled(this.enabled);
        }
        this.setAlignItems(this.alignItems);
        this.hideEnabledCheckbox(this.suppressEnabledCheckbox);
        this.hideOpenCloseIcons(this.suppressOpenCloseIcons);
        this.setupExpandContract();
        this.refreshChildDisplay();
    };
    DyGroupComponent.prototype.setupExpandContract = function () {
        var _this = this;
        this.eGroupClosedIcon.appendChild(createIcon('columnSelectClosed', this.gridOptionsWrapper, null));
        this.eGroupOpenedIcon.appendChild(createIcon('columnSelectOpen', this.gridOptionsWrapper, null));
        this.addManagedListener(this.eTitleBar, 'click', function () { return _this.toggleGroupExpand(); });
        this.addManagedListener(this.eTitleBar, 'keydown', function (e) {
            switch (e.keyCode) {
                case KeyCode.ENTER:
                    _this.toggleGroupExpand();
                    break;
                case KeyCode.RIGHT:
                    _this.toggleGroupExpand(true);
                    break;
                case KeyCode.LEFT:
                    _this.toggleGroupExpand(false);
                    break;
            }
        });
    };
    DyGroupComponent.prototype.refreshChildDisplay = function () {
        var showIcon = !this.suppressOpenCloseIcons;
        setDisplayed(this.eToolbar, this.expanded && !this.suppressEnabledCheckbox);
        setDisplayed(this.eGroupOpenedIcon, showIcon && this.expanded);
        setDisplayed(this.eGroupClosedIcon, showIcon && !this.expanded);
    };
    DyGroupComponent.prototype.isExpanded = function () {
        return this.expanded;
    };
    DyGroupComponent.prototype.setAlignItems = function (alignment) {
        var eGui = this.getGui();
        if (this.alignItems !== alignment) {
            removeCssClass(eGui, "dy-group-item-alignment-" + this.alignItems);
        }
        this.alignItems = alignment;
        var newCls = "dy-group-item-alignment-" + this.alignItems;
        addCssClass(eGui, newCls);
        return this;
    };
    DyGroupComponent.prototype.toggleGroupExpand = function (expanded) {
        if (this.suppressOpenCloseIcons) {
            this.expanded = true;
            this.refreshChildDisplay();
            setDisplayed(this.eContainer, true);
            return this;
        }
        expanded = expanded != null ? expanded : !this.expanded;
        if (this.expanded === expanded) {
            return this;
        }
        this.expanded = expanded;
        this.refreshChildDisplay();
        setDisplayed(this.eContainer, expanded);
        this.dispatchEvent({ type: this.expanded ? DyGroupComponent.EVENT_EXPANDED : DyGroupComponent.EVENT_COLLAPSED });
        return this;
    };
    DyGroupComponent.prototype.addItems = function (items) {
        var _this = this;
        items.forEach(function (item) { return _this.addItem(item); });
    };
    DyGroupComponent.prototype.addItem = function (item) {
        var container = this.eContainer;
        var el = item instanceof Component ? item.getGui() : item;
        addCssClass(el, 'dy-group-item');
        addCssClass(el, "dy-" + this.cssIdentifier + "-group-item");
        container.appendChild(el);
        this.items.push(el);
    };
    DyGroupComponent.prototype.hideItem = function (hide, index) {
        var itemToHide = this.items[index];
        addOrRemoveCssClass(itemToHide, 'dy-hidden', hide);
    };
    DyGroupComponent.prototype.setTitle = function (title) {
        this.eTitle.innerText = title;
        return this;
    };
    DyGroupComponent.prototype.addCssClassToTitleBar = function (cssClass) {
        addCssClass(this.eTitleBar, cssClass);
    };
    DyGroupComponent.prototype.setEnabled = function (enabled, skipToggle) {
        this.enabled = enabled;
        this.refreshDisabledStyles();
        this.toggleGroupExpand(enabled);
        if (!skipToggle) {
            this.cbGroupEnabled.setValue(enabled);
        }
        return this;
    };
    DyGroupComponent.prototype.isEnabled = function () {
        return this.enabled;
    };
    DyGroupComponent.prototype.onEnableChange = function (callbackFn) {
        var _this = this;
        this.cbGroupEnabled.onValueChange(function (newSelection) {
            _this.setEnabled(newSelection, true);
            callbackFn(newSelection);
        });
        return this;
    };
    DyGroupComponent.prototype.hideEnabledCheckbox = function (hide) {
        this.suppressEnabledCheckbox = hide;
        this.refreshChildDisplay();
        this.refreshDisabledStyles();
        return this;
    };
    DyGroupComponent.prototype.hideOpenCloseIcons = function (hide) {
        this.suppressOpenCloseIcons = hide;
        if (hide) {
            this.toggleGroupExpand(true);
        }
        return this;
    };
    DyGroupComponent.prototype.refreshDisabledStyles = function () {
        addOrRemoveCssClass(this.getGui(), 'dy-disabled', !this.enabled);
        if (this.suppressEnabledCheckbox && !this.enabled) {
            addCssClass(this.eTitleBar, 'dy-disabled-group-title-bar');
            this.eTitleBar.removeAttribute('tabindex');
        }
        else {
            removeCssClass(this.eTitleBar, 'dy-disabled-group-title-bar');
            this.eTitleBar.setAttribute('tabindex', '0');
        }
        addOrRemoveCssClass(this.eContainer, 'dy-disabled-group-container', !this.enabled);
    };
    DyGroupComponent.EVENT_EXPANDED = 'expanded';
    DyGroupComponent.EVENT_COLLAPSED = 'collapsed';
    __decorate([
        RefSelector('eTitleBar')
    ], DyGroupComponent.prototype, "eTitleBar", void 0);
    __decorate([
        RefSelector('eGroupOpenedIcon')
    ], DyGroupComponent.prototype, "eGroupOpenedIcon", void 0);
    __decorate([
        RefSelector('eGroupClosedIcon')
    ], DyGroupComponent.prototype, "eGroupClosedIcon", void 0);
    __decorate([
        RefSelector('eToolbar')
    ], DyGroupComponent.prototype, "eToolbar", void 0);
    __decorate([
        RefSelector('cbGroupEnabled')
    ], DyGroupComponent.prototype, "cbGroupEnabled", void 0);
    __decorate([
        RefSelector('eTitle')
    ], DyGroupComponent.prototype, "eTitle", void 0);
    __decorate([
        RefSelector('eContainer')
    ], DyGroupComponent.prototype, "eContainer", void 0);
    __decorate([
        PostConstruct
    ], DyGroupComponent.prototype, "postConstruct", null);
    return DyGroupComponent;
}(Component));
export { DyGroupComponent };
