/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement } from '@tylertech/forge-core';
import { CHIP_FIELD_CONSTANTS } from './chip-field-constants';
import { FieldAdapter } from '../field/field-adapter';
export class ChipFieldAdapter extends FieldAdapter {
    constructor(component) {
        super(component);
    }
    initialize() {
        super.initialize(CHIP_FIELD_CONSTANTS.selectors.ROOT);
        this._memberSlot = getShadowElement(this._component, CHIP_FIELD_CONSTANTS.selectors.MEMBER_SLOT);
        this._inputContainerElement = getShadowElement(this._component, CHIP_FIELD_CONSTANTS.selectors.INPUT_CONTAINER);
    }
    addRootListener(type, callback) {
        this._rootElement.addEventListener(type, callback);
    }
    removeRootListener(type, callback) {
        this._rootElement.removeEventListener(type, callback);
    }
    addMemberSlotListener(listener) {
        this._memberSlot.addEventListener('slotchange', listener);
    }
    removeMemberSlotListener(listener) {
        if (this._memberSlot) {
            this._memberSlot.removeEventListener('slotchange', listener);
        }
    }
    addInputContainerListener(type, callback) {
        this._inputContainerElement.addEventListener(type, callback);
    }
    removeInputContainerListener(type, callback) {
        this._inputContainerElement.removeEventListener(type, callback);
    }
    fieldHasValue() {
        const aMemberExists = !!this._component.querySelector(CHIP_FIELD_CONSTANTS.selectors.MEMBER);
        return aMemberExists || super.inputHasValue();
    }
    focusInput() {
        var _a;
        (_a = this._inputElement) === null || _a === void 0 ? void 0 : _a.focus();
    }
    tryPropagateClick(target) {
        var _a;
        // We only propagate the click to the input if it originated from our internal input container
        if (!this._inputElement.disabled && target instanceof HTMLElement && target.matches(CHIP_FIELD_CONSTANTS.selectors.INPUT_CONTAINER)) {
            (_a = this._inputElement) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new MouseEvent('click'));
        }
    }
    getSlottedMemberElements() {
        return this._component.querySelectorAll(CHIP_FIELD_CONSTANTS.selectors.MEMBER);
    }
}
