/* *
 *
 *  Grid Sort Toolbar Button class
 *
 *  (c) 2020-2026 Highsoft AS
 *
 *  A commercial license may be required depending on use.
 *  See www.highcharts.com/license
 *
 *
 *  Authors:
 *  - Dawid Dragula
 *
 * */
'use strict';
import ToolbarButton from '../../../../UI/ToolbarButton.js';
import GridUtils from '../../../../GridUtils.js';
import StateHelpers from '../StateHelpers.js';
import { addEvent } from '../../../../../../Shared/Utilities.js';
const { formatText } = GridUtils;
/* *
 *
 *  Class
 *
 * */
class SortToolbarButton extends ToolbarButton {
    getColumnLabel() {
        const column = this.toolbar?.column;
        const label = (column?.header?.headerContent?.textContent ||
            column?.header?.value ||
            column?.id ||
            '').trim();
        return label || column?.id || '';
    }
    updateA11yLabel(order, priority) {
        const button = this.wrapper?.querySelector('button');
        if (!button) {
            return;
        }
        const column = this.toolbar?.column;
        const lang = column?.viewport.grid.options?.lang;
        const sortingLang = lang?.accessibility?.sorting;
        const announcements = sortingLang?.announcements;
        const columnLabel = this.getColumnLabel();
        const labelParts = [];
        if (columnLabel) {
            labelParts.push(columnLabel);
        }
        let stateLabel;
        if (order === 'asc') {
            stateLabel = announcements?.ascending;
        }
        else if (order === 'desc') {
            stateLabel = announcements?.descending;
        }
        else {
            stateLabel = announcements?.none;
        }
        if (stateLabel) {
            labelParts.push(stateLabel);
        }
        if (priority) {
            labelParts.push(formatText(sortingLang?.priority ?? 'Priority {priority}.', { priority: String(priority) }));
        }
        if (labelParts.length) {
            button.setAttribute('aria-label', labelParts.join(' '));
        }
    }
    /* *
     *
     *  Constructor
     *
     * */
    constructor() {
        super({
            icon: 'arrowUpDown',
            accessibility: {
                ariaLabel: 'sort'
            }
        });
    }
    /* *
     *
     *  Methods
     *
     * */
    clickHandler(event) {
        super.clickHandler(event);
        this.toolbar?.column.sorting?.toggle(event);
    }
    renderSortPriorityIndicator(priority) {
        const wrapper = this.wrapper;
        if (!wrapper) {
            return;
        }
        const button = wrapper.querySelector('button');
        if (!button) {
            return;
        }
        if (!priority) {
            this.sortPriorityIndicator?.remove();
            delete this.sortPriorityIndicator;
            return;
        }
        if (!this.sortPriorityIndicator) {
            this.sortPriorityIndicator = document.createElement('span');
        }
        // Ensure the indicator is rendered to the right of the icon.
        button.appendChild(this.sortPriorityIndicator);
        this.sortPriorityIndicator.textContent = String(priority);
    }
    refreshState() {
        const column = this.toolbar?.column;
        if (!column) {
            return;
        }
        const { currentSortings, currentSorting } = column.viewport.grid.querying.sorting;
        const sortings = currentSortings || [];
        const columnSorting = (sortings.find((sorting) => sorting.columnId === column.id) ||
            (currentSorting?.columnId === column.id ?
                currentSorting :
                void 0));
        if (!StateHelpers.isSorted(column) || !columnSorting?.order) {
            this.setActive(false);
            this.setIcon('arrowUpDown');
            this.renderSortPriorityIndicator();
            this.updateA11yLabel(null);
            return;
        }
        this.setActive(true);
        this.setIcon(columnSorting.order === 'asc' ? 'arrowUp' : 'arrowDown');
        const sortIndex = sortings.findIndex((sorting) => sorting.columnId === column.id);
        const priority = (sortings.length > 1 && sortIndex !== -1 ?
            sortIndex + 1 :
            void 0);
        this.renderSortPriorityIndicator(priority);
        this.updateA11yLabel(columnSorting.order, priority);
    }
    addEventListeners() {
        super.addEventListeners();
        const column = this.toolbar?.column;
        if (!column) {
            return;
        }
        // If this grid is currently sorted, update the state
        this.eventListenerDestroyers.push(addEvent(column.viewport.grid, 'afterSort', () => this.refreshState()));
    }
}
/* *
 *
 *  Default Export
 *
 * */
export default SortToolbarButton;
