/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { getShadowElement, removeAllChildren } from '@tylertech/forge-core';
import { BaseAdapter } from '../core/base/base-adapter';
import { AVATAR_CONSTANTS } from './avatar-constants';
/**
 * The DOM adapter behind the `<forge-avatar>` element.
 */
export class AvatarAdapter extends BaseAdapter {
    constructor(component) {
        super(component);
        this._root = getShadowElement(this._component, AVATAR_CONSTANTS.selectors.ROOT);
        this._defaultSlot = getShadowElement(this._component, AVATAR_CONSTANTS.selectors.DEFAULT_SLOT);
    }
    /**
     * Sets the `backgroundColor` style on the content element.
     * @param {string} value The background color.
     */
    setBackgroundColor(value) {
        this._root.style.backgroundColor = `var(${AVATAR_CONSTANTS.strings.BACKGROUND_VARNAME}, ${value})`;
    }
    /**
     * Sets the background image URL.
     * @param url The URL.
     */
    setBackgroundImageUrl(url) {
        const backgroundColor = this._root.style.backgroundColor;
        // doing his before the promise so it doesn't flash a color before loading
        this._root.style.backgroundColor = 'inherit';
        const loadResult = new Promise(resolve => {
            const image = new Image();
            image.onload = () => {
                this._root.style.backgroundImage = `url(${image.src})`;
                resolve(true);
            };
            image.onerror = () => {
                this._root.style.backgroundColor = backgroundColor;
                resolve(false);
            };
            image.src = url;
        });
        return loadResult;
    }
    /**
     * Removes the background image URL.
     */
    removeBackgroundImage() {
        this._root.style.removeProperty('background-image');
    }
    /**
     * Sets the avatar text content.
     * @param value The text value.
     */
    setText(value) {
        this._defaultSlot.textContent = value;
    }
    clearText() {
        removeAllChildren(this._defaultSlot);
    }
}
