"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processIconString = exports.markdownItIconsPlugin = void 0;
// import the necessary packages
const octicons_1 = __importDefault(require("@primer/octicons"));
const cheerio = __importStar(require("cheerio"));
// markdown-it-regexp type definitions are undefined
// @ts-ignore
const markdown_it_regexp_1 = __importDefault(require("markdown-it-regexp"));
// regular expression to match the icon patterns
const ICON_REGEXP = /:(fa[brs]|fa-brands|fa-solid|glyphicon|octicon|octiconlight|mi[forst]|bi)-([a-z0-9-]+)~?([a-z0-9-]+)?:/;
// function to get the octicon icons
function getOcticonIcon(iconName) {
    var _a;
    // Indexing octicons varies based on @types/primer__octicons version
    // @ts-ignore
    return (_a = octicons_1.default[iconName]) !== null && _a !== void 0 ? _a : null;
}
// handler function for glyphicon icons
const handleGlyphicon = (iconFontName) => `<span aria-hidden="true" class="glyphicon glyphicon-${iconFontName}"></span>`;
// handler function for octicon icons
const handleOcticon = (iconFontName, iconClass) => {
    const octiconIcon = getOcticonIcon(iconFontName);
    // ensure octicons are valid
    if (octiconIcon === null) {
        return '<span aria-hidden="true"></span>';
    }
    return iconClass ? octiconIcon.toSVG({ class: iconClass }) : octiconIcon.toSVG();
};
// handler function for light octicon icons
const handleOcticonLight = (iconFontName, iconClass) => {
    const octiconIcon = getOcticonIcon(iconFontName);
    // ensure octicons are valid
    if (octiconIcon === null) {
        return '<span aria-hidden="true"></span>';
    }
    const octiconIconHtml = iconClass ? octiconIcon.toSVG({ class: iconClass }) : octiconIcon.toSVG();
    const $ = cheerio.load(octiconIconHtml);
    $('svg').attr('style', 'color: #fff');
    return $.html();
};
// handler function for material icons
const handleMaterialIcon = (iconFontType, iconFontName) => {
    let materialIconsClass = 'material-icons';
    switch (iconFontType) {
        case 'mio':
            materialIconsClass += '-outlined';
            break;
        case 'mir':
            materialIconsClass += '-round';
            break;
        case 'mis':
            materialIconsClass += '-sharp';
            break;
        case 'mit':
            materialIconsClass += '-two-tone';
            break;
        default:
        // .material-icons generates 'Filled' style icons; hence, no suffix is needed for 'mif'.
    }
    // Use .align-middle by default to vertically-align the icon with its surrounding text (if any).
    // Also, replace dashes (-) with underscores (_) to format the icon name properly.
    return `<span aria-hidden="true" class="${materialIconsClass} align-middle">`
        + `${iconFontName.split('-').join('_')}</span>`;
};
// handler function for font awesome icons
const handleFontAwesome = (iconFontType, iconFontName) => `<span aria-hidden="true" class="${iconFontType} fa-${iconFontName}"></span>`;
// Handler function for Bootstrap Icons
const handleBootstrapIcon = (iconFontName) => `<i class="bi-${iconFontName}"></i>`;
// function to get the respective icon html based on the icon font type
const getIconHtml = (match) => {
    const iconFontType = match[1];
    const iconFontName = match[2];
    const iconClass = match[3];
    if (iconFontType === 'glyphicon') {
        return handleGlyphicon(iconFontName);
    }
    else if (iconFontType === 'octicon') {
        return handleOcticon(iconFontName, iconClass);
    }
    else if (iconFontType === 'octiconlight') {
        return handleOcticonLight(iconFontName, iconClass);
    }
    else if (iconFontType.startsWith('mi')) {
        return handleMaterialIcon(iconFontType, iconFontName);
    }
    else if (iconFontType === 'bi') {
        return handleBootstrapIcon(iconFontName);
    }
    return handleFontAwesome(iconFontType, iconFontName);
};
// function to process the icon string and get the icon html
const processIconString = (iconStr) => {
    let icon = iconStr;
    if (!iconStr.startsWith(':') && !iconStr.endsWith(':')) {
        icon = `:${iconStr}:`;
    }
    const match = icon.match(ICON_REGEXP);
    return match ? getIconHtml(match) : null;
};
exports.processIconString = processIconString;
// create a markdown-it plugin to process the icon strings in the markdown
/**
 * A Markdown-It plugin that replaces icon syntax matching `ICON_REGEXP` with corresponding HTML.
 *
 * This plugin uses a regular expression to detect icon patterns in Markdown content,
 * and transforms them into HTML using the `getIconHtml` function.
 */
const markdownItIconsPlugin = (0, markdown_it_regexp_1.default)(ICON_REGEXP, (match) => getIconHtml(match));
exports.markdownItIconsPlugin = markdownItIconsPlugin;
//# sourceMappingURL=markdown-it-icons.js.map