"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IconsContributionPoint = exports.IconThemesContributionPoint = void 0;
const tslib_1 = require("tslib");
const di_1 = require("@opensumi/di");
const static_definition_1 = require("@opensumi/ide-core-browser/lib/static-resource/static.definition");
const ide_core_common_1 = require("@opensumi/ide-core-common");
const ide_theme_1 = require("@opensumi/ide-theme");
const icon_registry_1 = require("@opensumi/ide-theme/lib/common/icon-registry");
const common_1 = require("../../../common");
const types_1 = require("../../types");
let IconThemesContributionPoint = class IconThemesContributionPoint extends common_1.VSCodeContributePoint {
    contribute() {
        for (const contrib of this.contributesMap) {
            const { extensionId, contributes } = contrib;
            const themes = contributes.map((t) => ({
                ...t,
                label: this.getLocalizeFromNlsJSON(t.label, extensionId),
                extensionId,
            }));
            const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
            if (extension) {
                this.iconService.registerIconThemes(themes, ide_core_common_1.URI.from(extension.uri));
            }
        }
    }
};
exports.IconThemesContributionPoint = IconThemesContributionPoint;
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IIconService),
    tslib_1.__metadata("design:type", Object)
], IconThemesContributionPoint.prototype, "iconService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], IconThemesContributionPoint.prototype, "extensionManageService", void 0);
exports.IconThemesContributionPoint = IconThemesContributionPoint = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('iconThemes'),
    (0, common_1.LifeCycle)(2 /* LifeCyclePhase.Initialize */)
], IconThemesContributionPoint);
const formatMap = {
    ttf: 'truetype',
    woff: 'woff',
    woff2: 'woff2',
};
let IconsContributionPoint = class IconsContributionPoint extends common_1.VSCodeContributePoint {
    constructor() {
        super(...arguments);
        this.iconRegistry = (0, icon_registry_1.getIconRegistry)();
    }
    contribute() {
        for (const { extensionId, contributes } of this.contributesMap) {
            Object.keys(contributes).forEach((id) => {
                const icon = contributes[id];
                const extension = this.extensionManageService.getExtensionInstanceByExtId(extensionId);
                if (extension) {
                    const defaultIcon = icon.default;
                    if (typeof defaultIcon === 'string') {
                        this.iconRegistry.registerIcon(id, { id: defaultIcon }, icon.description);
                    }
                    else if (typeof defaultIcon === 'object' &&
                        typeof defaultIcon.fontPath === 'string' &&
                        typeof defaultIcon.fontCharacter === 'string') {
                        const fontPath = ide_core_common_1.URI.from(extension.uri).resolve(icon.default.fontPath);
                        const fileExt = ide_core_common_1.path.extname(defaultIcon.fontPath).substring(1);
                        const format = formatMap[fileExt];
                        if (!format) {
                            this.logger.warn((0, ide_core_common_1.localize)('invalid.icons.default.fontPath.extension', `Expected \`contributes.icons.default.fontPath\` to have file extension 'woff', woff2' or 'ttf', is '${fileExt}'.`));
                            return;
                        }
                        const fontId = ide_core_common_1.path.join(extensionId, defaultIcon.fontPath);
                        const definition = this.iconRegistry.registerIconFont(fontId, {
                            src: [{ location: this.staticResourceService.resolveStaticResource(fontPath), format }],
                        });
                        this.iconRegistry.registerIcon(id, {
                            fontCharacter: defaultIcon.fontCharacter,
                            font: {
                                id: fontId,
                                definition,
                            },
                        }, icon.description);
                    }
                }
            });
        }
        this.productIconService.updateProductIconThemes();
    }
};
exports.IconsContributionPoint = IconsContributionPoint;
tslib_1.__decorate([
    (0, di_1.Autowired)(types_1.AbstractExtInstanceManagementService),
    tslib_1.__metadata("design:type", types_1.AbstractExtInstanceManagementService)
], IconsContributionPoint.prototype, "extensionManageService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_theme_1.IProductIconService),
    tslib_1.__metadata("design:type", Object)
], IconsContributionPoint.prototype, "productIconService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(),
    tslib_1.__metadata("design:type", static_definition_1.StaticResourceService)
], IconsContributionPoint.prototype, "staticResourceService", void 0);
tslib_1.__decorate([
    (0, di_1.Autowired)(ide_core_common_1.ILogger),
    tslib_1.__metadata("design:type", Object)
], IconsContributionPoint.prototype, "logger", void 0);
exports.IconsContributionPoint = IconsContributionPoint = tslib_1.__decorate([
    (0, di_1.Injectable)(),
    (0, common_1.Contributes)('icons'),
    (0, common_1.LifeCycle)(2 /* LifeCyclePhase.Initialize */)
], IconsContributionPoint);
//# sourceMappingURL=icon.js.map