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 __());
    };
})();
import { RibbonGroupBase } from '../ribbon-interfaces';
import { RibbonItemType } from '@syncfusion/ej2-ribbon';
// Constants for UI element IDs
export var OPTIONS_GROUP_ID = '_options_group';
export var LINK_PREVIOUS_BUTTON_ID = '_link_previous_button';
export var DIFFERENT_FIRST_BUTTON_ID = '_different_first_button';
export var DIFFERENT_ODD_EVEN_BUTTON_ID = '_different_odd_even_button';
/**
 * Represents the Options Group in Header & Footer tab
 * @private
 */
var OptionsGroup = /** @class */ (function (_super) {
    __extends(OptionsGroup, _super);
    /**
     * Constructor for the OptionsGroup
     * @param {DocumentEditorContainer} container - DocumentEditorContainer instance
     */
    function OptionsGroup(container) {
        return _super.call(this, container) || this;
    }
    /**
     * Gets the ribbon group model for Options
     * @returns {RibbonGroupModel} The ribbon group model
     */
    OptionsGroup.prototype.getGroupModel = function () {
        var _this = this;
        return {
            id: this.ribbonId + OPTIONS_GROUP_ID,
            header: this.localObj.getConstant('Options'),
            enableGroupOverflow: true,
            overflowHeader: this.localObj.getConstant('Options'),
            collections: [
                {
                    items: [
                        {
                            type: RibbonItemType.CheckBox,
                            id: this.ribbonId + DIFFERENT_FIRST_BUTTON_ID,
                            keyTip: 'TF',
                            checkBoxSettings: {
                                label: this.localObj.getConstant('Different First Page'),
                                checked: false,
                                change: function (args) {
                                    if (!_this.documentEditor.isReadOnly) {
                                        var selection = _this.documentEditor.selectionModule;
                                        if (selection) {
                                            selection.sectionFormat.differentFirstPage = args.checked;
                                            setTimeout(function () {
                                                _this.documentEditor.focusIn();
                                            }, 10);
                                        }
                                    }
                                }
                            },
                            ribbonTooltipSettings: {
                                content: this.localObj.getConstant('Different header and footer for first page')
                            }
                        },
                        {
                            type: RibbonItemType.CheckBox,
                            id: this.ribbonId + DIFFERENT_ODD_EVEN_BUTTON_ID,
                            keyTip: 'TE',
                            checkBoxSettings: {
                                label: this.localObj.getConstant('Different Odd And Even Pages'),
                                checked: false,
                                change: function (args) {
                                    if (!_this.documentEditor.isReadOnly) {
                                        var selection = _this.documentEditor.selectionModule;
                                        if (selection) {
                                            selection.sectionFormat.differentOddAndEvenPages = args.checked;
                                            setTimeout(function () {
                                                _this.documentEditor.focusIn();
                                            }, 10);
                                        }
                                    }
                                }
                            },
                            ribbonTooltipSettings: {
                                content: this.localObj.getConstant('Different header and footer for odd and even pages')
                            }
                        },
                        {
                            type: RibbonItemType.CheckBox,
                            id: this.ribbonId + LINK_PREVIOUS_BUTTON_ID,
                            keyTip: 'TL',
                            checkBoxSettings: {
                                label: this.localObj.getConstant('Link to Previous'),
                                checked: false,
                                change: function (args) {
                                    if (!_this.documentEditor.isReadOnly) {
                                        var selection = _this.documentEditor.selectionModule;
                                        if (selection) {
                                            var headerFooterType = _this.getCurrentHeaderFooterType();
                                            if (headerFooterType) {
                                                _this.setLinkToPreviousValue(headerFooterType, args.checked);
                                            }
                                        }
                                    }
                                }
                            },
                            ribbonTooltipSettings: {
                                content: this.localObj.getConstant('Link to the previous sections header and footer')
                            }
                        }
                    ]
                }
            ]
        };
    };
    /**
     * Gets the current header/footer type
     * @returns {HeaderFooterType} The current header/footer type
     */
    OptionsGroup.prototype.getCurrentHeaderFooterType = function () {
        var selection = this.documentEditor.selectionModule;
        if (selection && selection.start && selection.start.paragraph &&
            selection.start.paragraph.containerWidget) {
            return selection.start.paragraph.containerWidget.headerFooterType;
        }
        return null;
    };
    /**
     * Checks if the current header/footer is linked to previous
     * @returns {boolean} Whether the current header/footer is linked to previous
     */
    OptionsGroup.prototype.isLinkToPreviousChecked = function () {
        var selection = this.documentEditor.selectionModule;
        if (!selection) {
            return false;
        }
        var headerFooterType = this.getCurrentHeaderFooterType();
        if (!headerFooterType) {
            return false;
        }
        switch (headerFooterType) {
            case 'OddHeader':
                return selection.sectionFormat.oddPageHeader.linkToPrevious;
            case 'OddFooter':
                return selection.sectionFormat.oddPageFooter.linkToPrevious;
            case 'EvenHeader':
                return selection.sectionFormat.evenPageHeader.linkToPrevious;
            case 'EvenFooter':
                return selection.sectionFormat.evenPageFooter.linkToPrevious;
            case 'FirstPageHeader':
                return selection.sectionFormat.firstPageHeader.linkToPrevious;
            case 'FirstPageFooter':
                return selection.sectionFormat.firstPageFooter.linkToPrevious;
            default:
                return false;
        }
    };
    /**
     * Sets the link to previous value for the current header/footer
     * @param {string} headerFooterType - The header/footer type
     * @param {boolean} value - The value to set
     * @returns {void}
     */
    OptionsGroup.prototype.setLinkToPreviousValue = function (headerFooterType, value) {
        var _this = this;
        var selection = this.documentEditor.selectionModule;
        if (!selection) {
            return;
        }
        switch (headerFooterType) {
            case 'OddHeader':
                selection.sectionFormat.oddPageHeader.linkToPrevious = value;
                break;
            case 'OddFooter':
                selection.sectionFormat.oddPageFooter.linkToPrevious = value;
                break;
            case 'EvenHeader':
                selection.sectionFormat.evenPageHeader.linkToPrevious = value;
                break;
            case 'EvenFooter':
                selection.sectionFormat.evenPageFooter.linkToPrevious = value;
                break;
            case 'FirstPageHeader':
                selection.sectionFormat.firstPageHeader.linkToPrevious = value;
                break;
            case 'FirstPageFooter':
                selection.sectionFormat.firstPageFooter.linkToPrevious = value;
                break;
        }
        setTimeout(function () {
            _this.documentEditor.focusIn();
        }, 10);
    };
    /**
     * Updates the checkbox states based on current document state
     * @returns {void}
     */
    OptionsGroup.prototype.updateSelection = function () {
        var ribbon = this.container.ribbon.ribbon;
        // Get checkbox elements
        var linkToPreviousCheckbox = ribbon.getItem(this.ribbonId + LINK_PREVIOUS_BUTTON_ID);
        var differentFirstCheckbox = ribbon.getItem(this.ribbonId + DIFFERENT_FIRST_BUTTON_ID);
        var differentOddEvenCheckbox = ribbon.getItem(this.ribbonId + DIFFERENT_ODD_EVEN_BUTTON_ID);
        var selection = this.documentEditor.selectionModule;
        if (selection) {
            // Update Different First Page checkbox
            if (differentFirstCheckbox) {
                var prevDifferentFirstCheckbox = differentFirstCheckbox.checkBoxSettings.checked;
                differentFirstCheckbox.checkBoxSettings.checked = selection.sectionFormat.differentFirstPage;
                if (prevDifferentFirstCheckbox !== differentFirstCheckbox.checkBoxSettings.checked) {
                    ribbon.updateItem(differentFirstCheckbox);
                }
            }
            // Update Different Odd & Even Pages checkbox
            if (differentOddEvenCheckbox) {
                var prevDifferentOddEvenCheckbox = differentOddEvenCheckbox.checkBoxSettings.checked;
                differentOddEvenCheckbox.checkBoxSettings.checked = selection.sectionFormat.differentOddAndEvenPages;
                if (prevDifferentOddEvenCheckbox !== differentOddEvenCheckbox.checkBoxSettings.checked) {
                    ribbon.updateItem(differentOddEvenCheckbox);
                }
            }
            // Update Link to Previous checkbox
            if (linkToPreviousCheckbox) {
                // Disable if this is the first section
                var isFirstSection = selection.start.paragraph.bodyWidget.sectionIndex === 0;
                var prevLinkToPreviousCheckbox = linkToPreviousCheckbox.checkBoxSettings.checked;
                if (isFirstSection) {
                    ribbon.disableItem(this.ribbonId + LINK_PREVIOUS_BUTTON_ID);
                }
                else {
                    ribbon.enableItem(this.ribbonId + LINK_PREVIOUS_BUTTON_ID);
                }
                if (!isFirstSection) {
                    var headerFooterType = this.getCurrentHeaderFooterType();
                    if (headerFooterType) {
                        linkToPreviousCheckbox.checkBoxSettings.checked = this.isLinkToPreviousChecked();
                    }
                }
                if (linkToPreviousCheckbox !== linkToPreviousCheckbox.checkBoxSettings.checked) {
                    ribbon.updateItem(linkToPreviousCheckbox);
                }
            }
        }
    };
    return OptionsGroup;
}(RibbonGroupBase));
export { OptionsGroup };
