import { RIBBON_ID } from './ribbon-constants';
import { isNullOrUndefined } from '@syncfusion/ej2-base';
/**
 * Manages ribbon state
 * @private
 */
var RibbonStateManager = /** @class */ (function () {
    /**
     * Constructor for RibbonStateManager
     * @param {DocumentEditorContainer} container - Document editor container
     */
    function RibbonStateManager(container) {
        this.container = container;
    }
    /**
     * Update ribbon state based on current selection
     * @param {Ribbon} ribbon - Ribbon instance
     * @returns {void}
     * @public
     */
    RibbonStateManager.prototype.updateRibbonState = function (ribbon) {
        if (!ribbon) {
            return;
        }
        var isProtectedDocument = this.container.documentEditor.documentHelper.protectionType !== 'NoProtection' || this.container.documentEditor.isReadOnly;
        var isSelectionInProtectedRegion = this.container.documentEditor.editorModule.restrictEditing;
        if (isProtectedDocument) {
            this.enableDisableRibbonItem(ribbon, !isSelectionInProtectedRegion && !this.container.documentEditor.isReadOnly);
        }
        else {
            this.enableDisableRibbonItem(ribbon, !isProtectedDocument);
        }
    };
    /**
     * Enable or disable ribbon items based on protection state
     * @param {Ribbon} ribbon - Ribbon instance
     * @param {boolean} enable - Whether to enable or disable items
     * @returns {void}
     * @private
     */
    RibbonStateManager.prototype.enableDisableRibbonItem = function (ribbon, enable) {
        if (!isNullOrUndefined(ribbon) && (!ribbon.tabs || !ribbon.tabs.length)) {
            return;
        }
        if (enable) {
            // Enable all tabs and groups
            for (var _i = 0, _a = ribbon.tabs; _i < _a.length; _i++) {
                var tab = _a[_i];
                ribbon.enableTab(tab.id);
                for (var _b = 0, _c = tab.groups; _b < _c.length; _b++) {
                    var group = _c[_b];
                    ribbon.enableGroup(group.id);
                }
            }
            this.container.ribbon.toggleTrackChanges();
            return;
        }
        // When disabling (enable === false)
        if (!isNullOrUndefined(ribbon) && !isNullOrUndefined(ribbon.tabs)) {
            for (var _d = 0, _e = ribbon.tabs; _d < _e.length; _d++) {
                var tab = _e[_d];
                var tabId = tab.id;
                var isViewTab = tabId.indexOf('view') !== -1;
                var isHomeTab = tabId.indexOf('home') !== -1;
                var isReviewTab = tabId.indexOf('review') !== -1;
                if (isReviewTab) {
                    ribbon.enableTab(tabId);
                    this.handleReviewTabProtection(ribbon, tab);
                }
                else if (isViewTab) {
                    ribbon.enableTab(tabId);
                    ribbon.disableGroup(this.container.element.id + RIBBON_ID + '_showGroup');
                }
                else if (isHomeTab) {
                    ribbon.enableTab(tabId);
                    this.handleHomeTabProtection(ribbon, tab);
                }
                else {
                    for (var _f = 0, _g = tab.groups; _f < _g.length; _f++) {
                        var group = _g[_f];
                        ribbon.disableGroup(group.id);
                    }
                }
            }
        }
    };
    /**
     * Handle home tab protection
     * @param {Ribbon} ribbon - Ribbon instance
     * @param {RibbonTabModel} tab - Tab object
     * @returns {void}
     * @private
     */
    RibbonStateManager.prototype.handleHomeTabProtection = function (ribbon, tab) {
        var homeGroups = tab.groups;
        for (var _i = 0, homeGroups_1 = homeGroups; _i < homeGroups_1.length; _i++) {
            var group = homeGroups_1[_i];
            var isFindGroup = group.id.indexOf('find') !== -1;
            if (isFindGroup) {
                ribbon.enableGroup(group.id);
                this.enableOnlyFindItems(ribbon, group);
            }
            else {
                ribbon.disableGroup(group.id);
            }
        }
    };
    /**
     * Enable only find-related items
     * @param {Ribbon} ribbon - Ribbon instance
     * @param {RibbonGroupModel} group - Group object
     * @returns {void}
     * @private
     */
    RibbonStateManager.prototype.enableOnlyFindItems = function (ribbon, group) {
        for (var _i = 0, _a = group.collections || []; _i < _a.length; _i++) {
            var collection = _a[_i];
            for (var _b = 0, _c = collection.items || []; _b < _c.length; _b++) {
                var item = _c[_b];
                var itemId = item.id;
                if (itemId && itemId.indexOf('find') !== -1) {
                    ribbon.enableItem(itemId);
                }
                else {
                    ribbon.disableItem(itemId);
                }
            }
        }
    };
    /**
     * Handle review tab protection
     * @param {Ribbon} ribbon - Ribbon instance
     * @param {RibbonTabModel} tab - Tab object
     * @returns {void}
     * @private
     */
    RibbonStateManager.prototype.handleReviewTabProtection = function (ribbon, tab) {
        var reviewGroups = tab.groups;
        for (var _i = 0, reviewGroups_1 = reviewGroups; _i < reviewGroups_1.length; _i++) {
            var group = reviewGroups_1[_i];
            var isRestrictedGroup = group.id.indexOf('comments') !== -1 ||
                group.id.indexOf('tracking') !== -1;
            if (group.id.indexOf('comments') !== -1) {
                var documentEditor = this.container.documentEditor;
                var enable = false;
                if (!isNullOrUndefined(documentEditor) && (documentEditor.isReadOnly ||
                    documentEditor.documentHelper.isDocumentProtected)) {
                    enable = documentEditor.documentHelper.isCommentOnlyMode || !documentEditor.isReadOnlyMode;
                }
                this.container.ribbon.enableDisableInsertComment(enable);
            }
            else if (isRestrictedGroup) {
                ribbon.disableGroup(group.id);
            }
            else {
                ribbon.enableGroup(group.id);
            }
        }
    };
    return RibbonStateManager;
}());
export { RibbonStateManager };
