
import { __decorate, __param } from '../../../../../../external/tslib/tslib.es6.js';
import { CancellationTokenSource } from '../../../base/common/cancellation.js';
import { Disposable } from '../../../base/common/lifecycle.js';
import { language } from '../../../base/common/platform.js';
import { localize } from '../../../nls.js';
import { IExtensionGalleryService } from '../../extensionManagement/common/extensionManagement.service.js';

function getLocale(extension) {
    return extension.tags.find(t => t.startsWith("lp-"))?.split("lp-")[1];
}
let LanguagePackBaseService = class LanguagePackBaseService extends Disposable {
    constructor(extensionGalleryService) {
        super();
        this.extensionGalleryService = extensionGalleryService;
    }
    async getAvailableLanguages() {
        const timeout = ( new CancellationTokenSource());
        setTimeout(() => timeout.cancel(), 1000);
        let result;
        try {
            result = await this.extensionGalleryService.query({
                text: "category:\"language packs\"",
                pageSize: 20
            }, timeout.token);
        } catch (_) {
            return [];
        }
        const languagePackExtensions = result.firstPage.filter(e => e.properties.localizedLanguages?.length && ( e.tags.some(t => t.startsWith("lp-"))));
        const allFromMarketplace = ( languagePackExtensions.map(lp => {
            const languageName = lp.properties.localizedLanguages?.[0];
            const locale = getLocale(lp);
            const baseQuickPick = this.createQuickPickItem(locale, languageName, lp);
            return {
                ...baseQuickPick,
                extensionId: lp.identifier.id,
                galleryExtension: lp
            };
        }));
        allFromMarketplace.push(this.createQuickPickItem("en", "English"));
        return allFromMarketplace;
    }
    createQuickPickItem(locale, languageName, languagePack) {
        const label = languageName ?? locale;
        let description;
        if (label !== locale) {
            description = `(${locale})`;
        }
        if (locale.toLowerCase() === language.toLowerCase()) {
            description ??= "";
            description += ( localize(1995, " (Current)"));
        }
        if (languagePack?.installCount) {
            description ??= "";
            const count = languagePack.installCount;
            let countLabel;
            if (count > 1000000) {
                countLabel = `${Math.floor(count / 100000) / 10}M`;
            } else if (count > 1000) {
                countLabel = `${Math.floor(count / 1000)}K`;
            } else {
                countLabel = String(count);
            }
            description += ` $(cloud-download) ${countLabel}`;
        }
        return {
            id: locale,
            label,
            description
        };
    }
};
LanguagePackBaseService = ( __decorate([( __param(0, IExtensionGalleryService))], LanguagePackBaseService));

export { LanguagePackBaseService, getLocale };
