import * as React from "react";
/**
 * Converts a Finsemble config entry to an Icon type that can be used to create <Icon> components.
 * @param config A finsemble icon config entry as derived by the launcher (e.g. "icons.toolBar", "icons.taskbar" or  "icons.titleBar")
 * @param title The title for the icon
 */
export const iconFromConfig = (config, title, appD) => {
    var _a, _b;
    if (((_a = appD === null || appD === void 0 ? void 0 : appD.icons) === null || _a === void 0 ? void 0 : _a.length) > 0 && appD.icons[0].src) {
        // mixed content would display a broken icon
        let mixedContent = !document.location.href.includes("file://") && ((_b = appD === null || appD === void 0 ? void 0 : appD.icons) === null || _b === void 0 ? void 0 : _b[0].src.includes("file://"));
        if (!mixedContent) {
            return {
                imageType: "url",
                path: appD.icons[0].src,
            };
        }
    }
    // Our default icon. The <Icon> component can generate an "initials" icon based just off of the `name`
    const defaultIcon = {
        imageType: "initials",
        name: title,
        category: "Application",
    };
    // Use iconography if available in the app's icon config entry
    if (config === null || config === void 0 ? void 0 : config.iconClass) {
        return {
            imageType: "fonticon",
            path: config.iconClass,
        };
    }
    if (config === null || config === void 0 ? void 0 : config.iconURL) {
        // mixed content would display a broken icon
        let mixedContent = !document.location.href.includes("file://") && (config === null || config === void 0 ? void 0 : config.iconURL.includes("file://"));
        if (!mixedContent) {
            return {
                imageType: "url",
                path: config.iconURL,
            };
        }
    }
    return defaultIcon;
};
const deriveClasses = (imageType, className = "") => {
    let classes = `icon-avatar-wrapper ${className}`;
    if (imageType === "url") {
        classes = `${classes} entity-with-img`;
    }
    else {
        classes = `${classes} icon-avatar`;
    }
    return classes;
};
/**
 * Derives two initials from a given name based on some simple heuristics.
 */
const avatarInitialsFromName = (name) => {
    // Significant words are over three characters
    const significantWords = name
        .toUpperCase()
        .split(" ")
        .flatMap((str) => (str.length > 3 ? [str] : []));
    // If no significant words then just return first two characters of the original name
    if (significantWords.length === 0)
        return name.substr(0, 2);
    // If only one significant word then return its first two characters
    if (significantWords.length === 1)
        return significantWords[0].substr(0, 2);
    // Return the first character of the first two significant words
    return significantWords[0].substr(0, 1).concat(significantWords[1].substr(0, 1));
};
/**
 * An icon to accompany Finsemble components or workspaces.
 * @param {string} [imageType] - "fonticon" | "url" | "initials" - Determines how the icon is generated.
 * @param {string} [path] - The path of the image.
 * @param {string} [name] - Used if imageType is "initials" to generate a text based icon.
 * @param {string} [category] - This will appear on the `data-category` attribute which can be used to drive CSS.
 * @param {string} [className] - Optional class to be appended.
 */
export const Icon = (props) => {
    var _a, _b, _c;
    let avatar;
    if (props.imageType === "initials") {
        const name = (_a = props.name) !== null && _a !== void 0 ? _a : "";
        avatar = React.createElement("span", null, avatarInitialsFromName(name));
    }
    if (props.imageType === "fonticon") {
        avatar = React.createElement("i", { className: props.path });
    }
    if (props.imageType === "url") {
        const mixedContent = !document.location.href.includes("file://") && ((_b = props.path) === null || _b === void 0 ? void 0 : _b.includes("file://"));
        if (mixedContent) {
            avatar = React.createElement("span", null, avatarInitialsFromName(props.name));
        }
        else {
            avatar = React.createElement("img", { src: props.path, alt: "" });
        }
    }
    const imageType = (_c = props.imageType) !== null && _c !== void 0 ? _c : "initials";
    const classes = deriveClasses(imageType, props.className);
    return (React.createElement("div", { className: classes, "data-category": props.category }, avatar));
};
//# sourceMappingURL=Icon.js.map