/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { addClass, getShadowElement, removeAllChildren, removeClass, toggleElementPlaceholder, toggleClass } from '@tylertech/forge-core';
import { BaseAdapter } from '../core/base/base-adapter';
import { PAGINATOR_CONSTANTS } from './paginator-constants';
/**
 * Provides facilities for interacting with the internal DOM of `PaginatorComponent`.
 */
export class PaginatorAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._labelElement = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.LABEL);
        this._root = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.ROOT);
        this._pageSizeSelect = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.PAGE_SIZE_SELECT);
        this._rangeLabel = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.RANGE_LABEL);
        this._rangeLabelAlternative = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.RANGE_LABEL_ALTERNATIVE);
        this._firstPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_BUTTON);
        this._firstPageIconButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_ICON_BUTTON);
        this._previousPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.PREVIOUS_PAGE_BUTTON);
        this._nextPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.NEXT_PAGE_BUTTON);
        this._lastPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_BUTTON);
        this._lastPageIconButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_ICON_BUTTON);
    }
    setLabel(value) {
        this._labelElement.textContent = value;
    }
    setPageSizeOptions(options) {
        removeAllChildren(this._pageSizeSelect);
        this._pageSizeSelect.options = options;
    }
    setPageSize(value) {
        this._pageSizeSelect.value = value.toString();
    }
    setRangeLabel(value) {
        this._rangeLabel.innerText = value;
        this._rangeLabelAlternative.innerText = value;
    }
    hasFirstPageButton() {
        return !!getShadowElement(this._component, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_ICON_BUTTON);
    }
    showFirstPageButton() {
        this._firstPagePlaceholder = toggleElementPlaceholder(this._component, true, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_ICON_BUTTON, this._firstPageIconButton, this._firstPagePlaceholder);
    }
    hideFirstPageButton() {
        this._firstPagePlaceholder = toggleElementPlaceholder(this._component, false, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_ICON_BUTTON, this._firstPageIconButton, this._firstPagePlaceholder);
    }
    hasLastPageButton() {
        return !!getShadowElement(this._component, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_ICON_BUTTON);
    }
    showLastPageButton() {
        this._lastPagePlaceholder = toggleElementPlaceholder(this._component, true, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_ICON_BUTTON, this._lastPageIconButton, this._lastPagePlaceholder);
    }
    hideLastPageButton() {
        this._lastPagePlaceholder = toggleElementPlaceholder(this._component, false, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_ICON_BUTTON, this._lastPageIconButton, this._lastPagePlaceholder);
    }
    attachPageSizeChangeListener(listener) {
        this._pageSizeSelect.addEventListener('change', listener);
    }
    attachFirstPageListener(listener) {
        this._firstPageButton.addEventListener('click', listener);
    }
    attachPreviousPageListener(listener) {
        this._previousPageButton.addEventListener('click', listener);
    }
    attachNextPageListener(listener) {
        this._nextPageButton.addEventListener('click', listener);
    }
    attachLastPageListener(listener) {
        this._lastPageButton.addEventListener('click', listener);
    }
    detachPageSizeChangeListener(listener) {
        this._pageSizeSelect.removeEventListener('change', listener);
    }
    detachFirstPageListener(listener) {
        this._firstPageButton.removeEventListener('click', listener);
    }
    detachPreviousPageListener(listener) {
        this._previousPageButton.removeEventListener('click', listener);
    }
    detachNextPageListener(listener) {
        this._nextPageButton.removeEventListener('click', listener);
    }
    detachLastPageListener(listener) {
        this._lastPageButton.removeEventListener('click', listener);
    }
    disableFirstPageButton() {
        this._firstPageButton.setAttribute('disabled', 'disabled');
    }
    enableFirstPageButton() {
        this._firstPageButton.removeAttribute('disabled');
    }
    disablePreviousPageButton() {
        this._previousPageButton.setAttribute('disabled', 'disabled');
    }
    enablePreviousPageButton() {
        this._previousPageButton.removeAttribute('disabled');
    }
    disableNextPageButton() {
        this._nextPageButton.setAttribute('disabled', 'disabled');
    }
    enableNextPageButton() {
        this._nextPageButton.removeAttribute('disabled');
    }
    disablePageSizeSelect() {
        this._pageSizeSelect.setAttribute('disabled', 'disabled');
    }
    enablePageSizeSelect() {
        this._pageSizeSelect.removeAttribute('disabled');
    }
    setPageSizeVisibility(visible) {
        if (visible) {
            this._pageSizeSelect.style.removeProperty('display');
        }
        else {
            this._pageSizeSelect.style.display = 'none';
        }
    }
    disableLastPageButton() {
        this._lastPageButton.setAttribute('disabled', 'disabled');
    }
    enableLastPageButton() {
        this._lastPageButton.removeAttribute('disabled');
    }
    setAlternative(alternative) {
        toggleClass(this._root, alternative, PAGINATOR_CONSTANTS.classes.ALTERNATIVE);
    }
    setAlignment(alignment) {
        removeClass([
            PAGINATOR_CONSTANTS.classes.ALIGNMENT_SPACE_BETWEEN,
            PAGINATOR_CONSTANTS.classes.ALIGNMENT_START,
            PAGINATOR_CONSTANTS.classes.ALIGNMENT_END
        ], this._root);
        switch (alignment) {
            case 'start':
                addClass(PAGINATOR_CONSTANTS.classes.ALIGNMENT_START, this._root);
                break;
            case 'end':
                addClass(PAGINATOR_CONSTANTS.classes.ALIGNMENT_END, this._root);
                break;
            case 'space-between':
            default:
                addClass(PAGINATOR_CONSTANTS.classes.ALIGNMENT_SPACE_BETWEEN, this._root);
                break;
        }
    }
    hasFocus() {
        return this._component.matches(':focus');
    }
    setFocus(options) {
        this._tryFocus([
            this._pageSizeSelect,
            this._firstPageButton,
            this._previousPageButton,
            this._nextPageButton,
            this._lastPageButton
        ], options);
    }
    getFocusedField() {
        if (this._pageSizeSelect.matches(':focus-within')) {
            return 'page-size';
        }
        else if (this._firstPageButton.matches(':focus-within')) {
            return 'first';
        }
        else if (this._previousPageButton.matches(':focus-within')) {
            return 'previous';
        }
        else if (this._nextPageButton.matches(':focus-within')) {
            return 'next';
        }
        else if (this._lastPageButton.matches(':focus-within')) {
            return 'last';
        }
        return null;
    }
    tryDisableFields(fieldsToDisable) {
        const fieldDisablers = {
            'first': () => this.disableFirstPageButton(),
            'last': () => this.disableLastPageButton(),
            'previous': () => this.disablePreviousPageButton(),
            'next': () => this.disableNextPageButton()
        };
        fieldsToDisable.forEach(field => { var _a; return (_a = fieldDisablers[field]) === null || _a === void 0 ? void 0 : _a.call(fieldDisablers); });
    }
    _tryFocus(elements, options) {
        const preventScroll = typeof (options === null || options === void 0 ? void 0 : options.preventScroll) === 'boolean' ? options.preventScroll : true;
        for (const el of elements) {
            if (el && el.isConnected && !el.disabled && el.style.display !== 'none') {
                el.focus(Object.assign(Object.assign({}, options), { preventScroll }));
                return;
            }
        }
    }
}
