/*!
 * KoliBri - The accessible HTML-Standard
 */
import { colorProp, labelProp, srcProp } from "../../props";
import { BaseController } from "../base-controller";
import { avatarPropsConfig } from "./api";
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);
};
export 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);
        });
    }
}
//# sourceMappingURL=controller.js.map
