/**
 * @license
 * Copyright Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, removeAllChildren, toggleElementPlaceholder } from '@tylertech/forge-core';
import { BaseAdapter } from '../core/base/base-adapter';
import { PAGINATOR_CONSTANTS } from './paginator-constants';
export class PaginatorAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._labelElement = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.LABEL);
        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._firstPageContainer = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_CONTAINER);
        this._previousPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.PREVIOUS_PAGE_BUTTON);
        this._previousPageContainer = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.PREVIOUS_PAGE_CONTAINER);
        this._nextPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.NEXT_PAGE_BUTTON);
        this._nextPageContainer = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.NEXT_PAGE_CONTAINER);
        this._lastPageButton = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_BUTTON);
        this._lastPageContainer = getShadowElement(component, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_CONTAINER);
    }
    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_BUTTON);
    }
    showFirstPageButton() {
        this._firstPagePlaceholder = toggleElementPlaceholder(this._component, true, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_CONTAINER, this._firstPageContainer, this._firstPagePlaceholder);
    }
    hideFirstPageButton() {
        this._firstPagePlaceholder = toggleElementPlaceholder(this._component, false, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.FIRST_PAGE_CONTAINER, this._firstPageContainer, this._firstPagePlaceholder);
    }
    hasLastPageButton() {
        return !!getShadowElement(this._component, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_BUTTON);
    }
    showLastPageButton() {
        this._lastPagePlaceholder = toggleElementPlaceholder(this._component, true, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_CONTAINER, this._lastPageContainer, this._lastPagePlaceholder);
    }
    hideLastPageButton() {
        this._lastPagePlaceholder = toggleElementPlaceholder(this._component, false, PAGINATOR_CONSTANTS.elementName, PAGINATOR_CONSTANTS.selectors.LAST_PAGE_CONTAINER, this._lastPageContainer, this._lastPagePlaceholder);
    }
    attachPageSizeChangeListener(listener) {
        this._pageSizeSelect.addEventListener('change', listener);
    }
    detachPageSizeChangeListener(listener) {
        this._pageSizeSelect.removeEventListener('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);
    }
    setFirstPageButtonEnabled(enabled) {
        this._firstPageButton.disabled = !enabled;
    }
    setPreviousPageButtonEnabled(enabled) {
        this._previousPageButton.disabled = !enabled;
    }
    setNextPageButtonEnabled(enabled) {
        this._nextPageButton.disabled = !enabled;
    }
    setLastPageButtonEnabled(enabled) {
        this._lastPageButton.disabled = !enabled;
    }
    setPageSizeSelectEnabled(enabled) {
        this._pageSizeSelect.disabled = !enabled;
    }
    setPageSizeVisibility(visible) {
        this._pageSizeSelect.hidden = !visible;
    }
    setFocus(options) {
        this._tryFocus([
            this._pageSizeSelect,
            this._firstPageButton,
            this._previousPageButton,
            this._nextPageButton,
            this._lastPageButton
        ], options);
    }
    tryDisableFields(fieldsToDisable) {
        const fieldDisablers = {
            'first': () => this.setFirstPageButtonEnabled(false),
            'last': () => this.setLastPageButtonEnabled(false),
            'previous': () => this.setPreviousPageButtonEnabled(false),
            'next': () => this.setNextPageButtonEnabled(false)
        };
        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;
            }
        }
    }
}
