/*!
 * KoliBri - The accessible HTML-Standard
 */
import { h, r as registerInstance, H as Host } from './index-Brmny3Vg.js';
import { t as translate } from './i18n-CaL2ODdQ.js';
import { b as bem } from './bem-registry-B9CzNnO1.js';
import './common-DEEjER7G.js';
import { c as createPropDefinition } from './normalizers-pgU77mXp.js';
import { c as createContrastColorPair } from './contrast-C0oqtcfU.js';
import { l as labelProp } from './label-DPd4-fag.js';
import { B as BaseWebComponent } from './variant-quote-BMV8GZX8.js';
import { s as srcProp } from './src-C8dP-FLK.js';
import { B as BaseController } from './base-controller-CXhqh4cR.js';
import './i18n-B2d_exHc.js';
import './_Uint8Array-kJHDjtoP.js';
import './isArray-CcrBs4JM.js';

const HEX_REGEX = /^#((\d|[a-f]){8}|(\d|[a-f]){6}|(\d|[a-f]){3,4})$/i;
function isHexString(value) {
    return HEX_REGEX.test(value);
}
function normalizer(value) {
    if (typeof value === 'string' && isHexString(value)) {
        const colors = createContrastColorPair(value);
        return {
            backgroundColor: colors.background,
            foregroundColor: colors.foreground,
        };
    }
    else if (typeof value === 'object' && value) {
        const colorPair = value;
        if (typeof colorPair.backgroundColor === 'string' &&
            typeof colorPair.foregroundColor === 'string' &&
            isHexString(colorPair.backgroundColor) &&
            isHexString(colorPair.foregroundColor)) {
            const colors = createContrastColorPair({
                background: colorPair.backgroundColor,
                foreground: colorPair.foregroundColor,
            });
            return {
                backgroundColor: colors.background,
                foregroundColor: colors.foreground,
            };
        }
    }
    throw new Error(`Invalid color ${value}`);
}
function validator(value) {
    return isHexString(value.foregroundColor) && isHexString(value.backgroundColor);
}
const colorProp = createPropDefinition('color', {
    backgroundColor: '#d3d3d3',
    foregroundColor: '#3f3f3f',
}, normalizer, validator);

const BEM_BLOCK_AVATAR = 'kol-avatar';
const BEM_CLASS_AVATAR__IMAGE = bem(BEM_BLOCK_AVATAR, 'image');
const BEM_CLASS_AVATAR__INITIALS = bem(BEM_BLOCK_AVATAR, 'initials');
const AvatarFC = (props) => {
    const { color, initials, label, src } = props;
    return (h("div", { "aria-label": translate('kol-avatar-alt', { placeholders: { name: label } }), class: "kol-avatar", role: "img", style: {
            backgroundColor: color.backgroundColor,
            color: color.foregroundColor,
        } }, src ? (h("img", { alt: "", "aria-hidden": "true", class: BEM_CLASS_AVATAR__IMAGE, src: src })) : (h("span", { "aria-hidden": "true", class: BEM_CLASS_AVATAR__INITIALS }, initials))));
};

const avatarPropsConfig = {
    optional: [colorProp, srcProp],
    required: [labelProp],
};

const formatNameAsInitial = (name) => {
    if (name.length === 0) {
        return '';
    }
    return name[0].toUpperCase();
};
const normalizeInitials = (value) => {
    const names = value.trim().split(/\s+/);
    const first = names[0];
    const last = names[names.length - 1];
    if (names.length >= 2 && first && last) {
        return `${formatNameAsInitial(first)}${formatNameAsInitial(last)}`;
    }
    return formatNameAsInitial(value);
};
class AvatarController extends BaseController {
    constructor(stateAccess) {
        super(stateAccess, avatarPropsConfig);
    }
    componentWillLoad(props) {
        const { color, label, src } = props;
        this.watchColor(color);
        this.watchLabel(label);
        this.watchSrc(src);
    }
    watchColor(value) {
        colorProp.apply(value, (v) => {
            this.setRenderProp('color', v);
        });
    }
    watchLabel(value) {
        labelProp.apply(value, (v) => {
            this.setRenderProp('label', v);
            this.setState('initials', normalizeInitials(v));
        });
    }
    watchSrc(value) {
        srcProp.apply(value, (v) => {
            this.setRenderProp('src', v);
        });
    }
}

const defaultStyleCss = "/* forward the rem function */\n/*\n* This file defines the layer order for all CSS layers used in KoliBri.\n* The order is important as it determines the cascade priority.\n*\n* Layer order (lowest to highest priority):\n* 1. kol-a11y - Accessibility defaults and requirements\n* 2. kol-global - Global component styles and resets\n* 3. kol-component - Component-specific styles\n* 4. kol-theme-global - Theme-specific global styles\n* 5. kol-theme-component - Theme-specific component styles\n*/\n@layer kol-a11y, kol-global, kol-component, kol-theme-global, kol-theme-component;\n/*\n * This file contains all rules for accessibility.\n */\n@layer kol-a11y {\n  :host {\n    /*\n                   * Minimum size of interactive elements.\n                   */\n    --a11y-min-size: calc(44 * 1rem / var(--kolibri-root-font-size, 16));\n    /*\n     * No element should be used without verifying the contrast ratio of its background and font colors.\n     * By initially setting the background color to white and the font color to black,\n     * the contrast ratio is ensured and explicit adjustment is forced.\n     */\n    color: black;\n    background-color: white;\n    /*\n     * Verdana is an accessible font that can be used without requiring additional loading time.\n     */\n    font-family: Verdana;\n    /*\n     * Letter spacing is required for all texts.\n     */\n    letter-spacing: inherit;\n    /*\n     * Word spacing is required for all texts.\n     */\n    word-spacing: inherit;\n    /*\n     * Text should be aligned left by default to provide a predictable starting point.\n     */\n    text-align: left;\n  }\n  * {\n    /*\n     * This rule enables the word dividing for all texts. That is important for high zoom levels.\n     */\n    hyphens: auto;\n    /*\n     * This rule enables the word dividing for all texts. That is important for high zoom levels.\n     */\n    word-break: break-word;\n  }\n  /*\n   * All interactive elements should have a minimum size of to-rem(44).\n   */\n  /* input:not([type='checkbox'], [type='radio'], [type='range']), */\n  /* option, */\n  /* select, */\n  /* textarea, */\n  button,\n  .kol-input .input {\n    min-width: var(--a11y-min-size);\n    min-height: var(--a11y-min-size);\n  }\n  /*\n   * Some interactive elements should not inherit the font-family and font-size.\n   */\n  a,\n  button,\n  h1,\n  h2,\n  h3,\n  h4,\n  h5,\n  h6,\n  input,\n  option,\n  select,\n  textarea {\n    /*\n     * All elements should inherit the text color from his parent element.\n     */\n    color: inherit;\n    /*\n     * All elements should inherit the font family from his parent element.\n     */\n    font-family: inherit;\n    /*\n     * All elements should inherit the font size from his parent element.\n     */\n    font-size: inherit;\n    /*\n     * Letter spacing is required for all texts.\n     */\n    letter-spacing: inherit;\n    /*\n     * Word spacing is required for all texts.\n     */\n    word-spacing: inherit;\n  }\n  /**\n  * Sometimes we need the semantic element for accessibility reasons,\n  * but we don't want to show it.\n  *\n  * - https://www.a11yproject.com/posts/how-to-hide-content/\n  */\n  .visually-hidden {\n    position: fixed;\n    top: 0;\n    left: 0;\n    width: 1px;\n    height: 1px;\n    overflow: hidden;\n    white-space: nowrap;\n    clip-path: inset(50%);\n  }\n}\n@layer kol-global {\n  /*\n   * Dieses CSS stellt sicher, dass der Standard-Style\n   * von A und Button resettet werden.\n   */\n  :is(a, button) {\n    background-color: transparent;\n    width: 100%;\n    margin: 0;\n    padding: 0;\n    border: none;\n    /* 100% needed for custom width from outside */\n  }\n  /*\n   * Ensure elements with hidden attribute to be actually not visible\n   * @see https://meowni.ca/hidden.is.a.lie.html\n   */\n  [hidden] {\n    display: none !important;\n  }\n  .badge-text-hint {\n    color: black;\n    background-color: white;\n  }\n}\n@layer kol-global {\n  :host {\n    /*\n     * The max-width is needed to prevent the table from overflowing the\n     * parent node, if the table is wider than the parent node.\n     */\n    max-width: 100%;\n    font-size: calc(16 * 1rem / var(--kolibri-root-font-size, 16));\n  }\n  * {\n    /*\n     * We prefer to box-sizing: border-box for all elements.\n     */\n    box-sizing: border-box;\n  }\n  .kol-span {\n    /* KolSpan is a layout component with icons in all directions and a label text in the middle. */\n    display: flex;\n    flex-flow: column;\n    align-items: center;\n    justify-content: center;\n    /* The sub span in KolSpan is the horizontal span with icon left and right and the label text in the middle. */\n  }\n  .kol-span__container {\n    display: flex;\n    align-items: center;\n  }\n  a,\n  button {\n    cursor: pointer;\n  }\n  .kol-span .kol-span__label--hide-label .kol-span__label {\n    display: none;\n  }\n  /* Reset browser agent style. */\n  button:disabled {\n    color: unset;\n  }\n  .disabled label,\n  .disabled:focus-within label,\n  [aria-disabled=true],\n  [aria-disabled=true]:focus,\n  [disabled],\n  [disabled]:focus {\n    outline: none;\n    cursor: not-allowed;\n  }\n  [aria-disabled=true]:focus .kol-span,\n  [disabled]:focus .kol-span {\n    outline: none !important;\n  }\n  .hastooltip {\n    z-index: 900 !important;\n  }\n}\n@layer kol-component {\n  :host {\n    display: block;\n  }\n}\n@layer kol-component {\n  .kol-avatar,\n  .kol-avatar__image,\n  .kol-avatar__initials {\n    border-radius: 50%;\n  }\n  .kol-avatar {\n    /* Visible with forced colors  */\n    outline: transparent solid calc(1 * 1rem / var(--kolibri-root-font-size, 16));\n    display: flex;\n    width: calc(100 * 1rem / var(--kolibri-root-font-size, 16));\n    height: calc(100 * 1rem / var(--kolibri-root-font-size, 16));\n  }\n  .kol-avatar__initials {\n    background-color: inherit;\n    margin: auto;\n    font-size: calc(40 * 1rem / var(--kolibri-root-font-size, 16));\n  }\n}";

const KolAvatar = class extends BaseWebComponent {
    constructor(hostRef) {
        super();
        registerInstance(this, hostRef);
        this.ctrl = new AvatarController(this.stateAccess);
        this.initials = '';
    }
    watchColor(value) {
        this.ctrl.watchColor(value);
    }
    watchLabel(value) {
        this.ctrl.watchLabel(value);
    }
    watchSrc(value) {
        this.ctrl.watchSrc(value);
    }
    componentWillLoad() {
        this.ctrl.componentWillLoad({
            color: this._color,
            label: this._label,
            src: this._src,
        });
    }
    render() {
        return (h(Host, { key: '293a0f642334d4058d3b129b69d7de334bb43866' }, h(AvatarFC, { key: '96c92705c57c43050b666513dfc65829a7be5047', color: this.ctrl.getRenderProp('color'), label: this.ctrl.getRenderProp('label'), src: this.ctrl.getRenderProp('src'), initials: this.initials })));
    }
    static get watchers() { return {
        "_color": ["watchColor"],
        "_label": ["watchLabel"],
        "_src": ["watchSrc"]
    }; }
};
KolAvatar.style = {
    default: defaultStyleCss
};

export { KolAvatar as kol_avatar };
//# sourceMappingURL=kol-avatar.entry.js.map
