/**
 * @license
 * Copyright 2023 Tyler Technologies, Inc. 
 * License: Apache-2.0
 */
import { coerceNumber } from '@tylertech/forge-core';
import { COLOR_CONSTANTS } from '../theme';
import { PRODUCT_ICON_CONSTANTS } from './product-icon-constants';
export class ProductIconFoundation {
    constructor(_adapter) {
        this._adapter = _adapter;
        this._colorValue = PRODUCT_ICON_CONSTANTS.strings.DEFAULT_COLOR_VALUE;
        this._colorPalette = PRODUCT_ICON_CONSTANTS.strings.DEFAULT_COLOR_PALETTE;
        this._colorShade = PRODUCT_ICON_CONSTANTS.strings.DEFAULT_COLOR_SHADE;
        this._size = PRODUCT_ICON_CONSTANTS.numbers.DEFAULT_SIZE;
        this._shadow = true;
        this._backgroundColor = PRODUCT_ICON_CONSTANTS.strings.DEFAULT_BACKGROUND_COLOR;
        this._iterations = PRODUCT_ICON_CONSTANTS.numbers.DEFAULT_ITERATIONS;
        this._usingCustomIterations = false;
    }
    initialize() {
        this._adapter.setElementSize(this._size);
        this._updateFontSize();
        this._parseColor();
        this._render();
    }
    _parseColor() {
        if (this._colorValue.includes('-')) {
            this._colorPalette = this._colorValue.split('-')[0];
            this._colorShade = coerceNumber(this._colorValue.split('-')[1]);
        }
        else {
            this._colorPalette = this._colorValue;
            this._colorShade = PRODUCT_ICON_CONSTANTS.strings.DEFAULT_COLOR_SHADE;
        }
    }
    _updateSize() {
        this._adapter.setElementSize(this._size);
        // If not using a custom iteration value, then we need to adjust the shadow length
        if (!this._usingCustomIterations) {
            this._iterations = this._size / 2;
            this._updateShadow();
        }
    }
    _updateFontSize() {
        this._adapter.setFontSize(this._size);
    }
    _updateShadow() {
        if (this._shadow) {
            this._adapter.setTextShadow(this._shadow, this._iterations, this._shadowColor);
        }
    }
    _render() {
        if (!this._colorValue) {
            return;
        }
        const iconColor = this._colorShade <= PRODUCT_ICON_CONSTANTS.numbers.ACCESSIBILITY_COLOR_THRESHOLD ? '#000000' : '#ffffff';
        const shadowIncrement = this._colorShade + 300;
        if (!COLOR_CONSTANTS[`${this._colorPalette}Palette`]) {
            this._colorPalette = PRODUCT_ICON_CONSTANTS.strings.DEFAULT_COLOR_PALETTE;
        }
        this._backgroundColor = COLOR_CONSTANTS[`${this._colorPalette}Palette`][this._colorShade];
        this._shadowColor = COLOR_CONSTANTS[`${this._colorPalette}Palette`][shadowIncrement];
        this._adapter.setBackgroundColor(this._backgroundColor);
        this._adapter.setIconColor(iconColor);
        this._updateShadow();
    }
    get color() {
        return this._colorValue;
    }
    set color(value) {
        if (this._colorValue !== value) {
            this._colorValue = value;
            this._parseColor();
            this._render();
            this._adapter.setHostAttribute(PRODUCT_ICON_CONSTANTS.attributes.COLOR, this._colorValue);
        }
    }
    get size() {
        return this._size;
    }
    set size(value) {
        if (this._size !== value) {
            this._size = value;
            this._updateSize();
            this._updateFontSize();
            this._adapter.setHostAttribute(PRODUCT_ICON_CONSTANTS.attributes.SIZE, this._size.toString());
        }
    }
    get shadow() {
        return this._shadow;
    }
    set shadow(value) {
        if (this._shadow !== value) {
            this._shadow = value;
            this._adapter.setTextShadow(this._shadow, this._iterations, this._shadowColor);
            if (this._shadow) {
                this._adapter.setHostAttribute(PRODUCT_ICON_CONSTANTS.attributes.SHADOW, '');
            }
            else {
                this._adapter.removeIconClass(PRODUCT_ICON_CONSTANTS.attributes.SHADOW);
            }
        }
    }
    get iterations() {
        return this._iterations;
    }
    set iterations(value) {
        if (this._iterations !== value) {
            this._usingCustomIterations = true;
            this._iterations = value;
            this._updateShadow();
            this._adapter.setHostAttribute(PRODUCT_ICON_CONSTANTS.attributes.ITERATIONS, this._iterations.toString());
        }
    }
}
