
import { Platform } from '../../../base/common/platform.js';
import { localize2 } from '../../../nls.js';
import { TargetPlatform } from '../../extensions/common/extensions.js';
import { FileOperationResult } from '../../files/common/files.js';

const EXTENSION_IDENTIFIER_PATTERN = '^([a-z0-9A-Z][a-z0-9-A-Z]*)\\.([a-z0-9A-Z][a-z0-9-A-Z]*)$';
const EXTENSION_IDENTIFIER_REGEX = ( new RegExp(EXTENSION_IDENTIFIER_PATTERN));
const WEB_EXTENSION_TAG = '__web_extension';
const EXTENSION_INSTALL_SKIP_WALKTHROUGH_CONTEXT = 'skipWalkthrough';
const EXTENSION_INSTALL_SKIP_PUBLISHER_TRUST_CONTEXT = 'skipPublisherTrust';
const EXTENSION_INSTALL_SOURCE_CONTEXT = 'extensionInstallSource';
const EXTENSION_INSTALL_DEP_PACK_CONTEXT = 'dependecyOrPackExtensionInstall';
var ExtensionInstallSource;
(function (ExtensionInstallSource) {
    ExtensionInstallSource["COMMAND"] = "command";
    ExtensionInstallSource["SETTINGS_SYNC"] = "settingsSync";
})(ExtensionInstallSource || (ExtensionInstallSource = {}));
function TargetPlatformToString(targetPlatform) {
    switch (targetPlatform) {
        case TargetPlatform.WIN32_X64: return 'Windows 64 bit';
        case TargetPlatform.WIN32_ARM64: return 'Windows ARM';
        case TargetPlatform.LINUX_X64: return 'Linux 64 bit';
        case TargetPlatform.LINUX_ARM64: return 'Linux ARM 64';
        case TargetPlatform.LINUX_ARMHF: return 'Linux ARM';
        case TargetPlatform.ALPINE_X64: return 'Alpine Linux 64 bit';
        case TargetPlatform.ALPINE_ARM64: return 'Alpine ARM 64';
        case TargetPlatform.DARWIN_X64: return 'Mac';
        case TargetPlatform.DARWIN_ARM64: return 'Mac Silicon';
        case TargetPlatform.WEB: return 'Web';
        case TargetPlatform.UNIVERSAL: return TargetPlatform.UNIVERSAL;
        case TargetPlatform.UNKNOWN: return TargetPlatform.UNKNOWN;
        case TargetPlatform.UNDEFINED: return TargetPlatform.UNDEFINED;
    }
}
function toTargetPlatform(targetPlatform) {
    switch (targetPlatform) {
        case TargetPlatform.WIN32_X64: return TargetPlatform.WIN32_X64;
        case TargetPlatform.WIN32_ARM64: return TargetPlatform.WIN32_ARM64;
        case TargetPlatform.LINUX_X64: return TargetPlatform.LINUX_X64;
        case TargetPlatform.LINUX_ARM64: return TargetPlatform.LINUX_ARM64;
        case TargetPlatform.LINUX_ARMHF: return TargetPlatform.LINUX_ARMHF;
        case TargetPlatform.ALPINE_X64: return TargetPlatform.ALPINE_X64;
        case TargetPlatform.ALPINE_ARM64: return TargetPlatform.ALPINE_ARM64;
        case TargetPlatform.DARWIN_X64: return TargetPlatform.DARWIN_X64;
        case TargetPlatform.DARWIN_ARM64: return TargetPlatform.DARWIN_ARM64;
        case TargetPlatform.WEB: return TargetPlatform.WEB;
        case TargetPlatform.UNIVERSAL: return TargetPlatform.UNIVERSAL;
        default: return TargetPlatform.UNKNOWN;
    }
}
function getTargetPlatform(platform, arch) {
    switch (platform) {
        case Platform.Windows:
            if (arch === 'x64') {
                return TargetPlatform.WIN32_X64;
            }
            if (arch === 'arm64') {
                return TargetPlatform.WIN32_ARM64;
            }
            return TargetPlatform.UNKNOWN;
        case Platform.Linux:
            if (arch === 'x64') {
                return TargetPlatform.LINUX_X64;
            }
            if (arch === 'arm64') {
                return TargetPlatform.LINUX_ARM64;
            }
            if (arch === 'arm') {
                return TargetPlatform.LINUX_ARMHF;
            }
            return TargetPlatform.UNKNOWN;
        case 'alpine':
            if (arch === 'x64') {
                return TargetPlatform.ALPINE_X64;
            }
            if (arch === 'arm64') {
                return TargetPlatform.ALPINE_ARM64;
            }
            return TargetPlatform.UNKNOWN;
        case Platform.Mac:
            if (arch === 'x64') {
                return TargetPlatform.DARWIN_X64;
            }
            if (arch === 'arm64') {
                return TargetPlatform.DARWIN_ARM64;
            }
            return TargetPlatform.UNKNOWN;
        case Platform.Web: return TargetPlatform.WEB;
    }
}
function isNotWebExtensionInWebTargetPlatform(allTargetPlatforms, productTargetPlatform) {
    return productTargetPlatform === TargetPlatform.WEB && !allTargetPlatforms.includes(TargetPlatform.WEB);
}
function isTargetPlatformCompatible(extensionTargetPlatform, allTargetPlatforms, productTargetPlatform) {
    if (isNotWebExtensionInWebTargetPlatform(allTargetPlatforms, productTargetPlatform)) {
        return false;
    }
    if (extensionTargetPlatform === TargetPlatform.UNDEFINED) {
        return true;
    }
    if (extensionTargetPlatform === TargetPlatform.UNIVERSAL) {
        return true;
    }
    if (extensionTargetPlatform === TargetPlatform.UNKNOWN) {
        return false;
    }
    if (extensionTargetPlatform === productTargetPlatform) {
        return true;
    }
    return false;
}
function isIExtensionIdentifier(thing) {
    return thing
        && typeof thing === 'object'
        && typeof thing.id === 'string'
        && (!thing.uuid || typeof thing.uuid === 'string');
}
var SortBy;
(function (SortBy) {
    SortBy["NoneOrRelevance"] = "NoneOrRelevance";
    SortBy["LastUpdatedDate"] = "LastUpdatedDate";
    SortBy["Title"] = "Title";
    SortBy["PublisherName"] = "PublisherName";
    SortBy["InstallCount"] = "InstallCount";
    SortBy["PublishedDate"] = "PublishedDate";
    SortBy["AverageRating"] = "AverageRating";
    SortBy["WeightedRating"] = "WeightedRating";
})(SortBy || (SortBy = {}));
var SortOrder;
(function (SortOrder) {
    SortOrder[SortOrder["Default"] = 0] = "Default";
    SortOrder[SortOrder["Ascending"] = 1] = "Ascending";
    SortOrder[SortOrder["Descending"] = 2] = "Descending";
})(SortOrder || (SortOrder = {}));
var FilterType;
(function (FilterType) {
    FilterType["Category"] = "Category";
    FilterType["ExtensionId"] = "ExtensionId";
    FilterType["ExtensionName"] = "ExtensionName";
    FilterType["ExcludeWithFlags"] = "ExcludeWithFlags";
    FilterType["Featured"] = "Featured";
    FilterType["SearchText"] = "SearchText";
    FilterType["Tag"] = "Tag";
    FilterType["Target"] = "Target";
})(FilterType || (FilterType = {}));
var StatisticType;
(function (StatisticType) {
    StatisticType["Install"] = "install";
    StatisticType["Uninstall"] = "uninstall";
})(StatisticType || (StatisticType = {}));
var InstallOperation;
(function (InstallOperation) {
    InstallOperation[InstallOperation["None"] = 1] = "None";
    InstallOperation[InstallOperation["Install"] = 2] = "Install";
    InstallOperation[InstallOperation["Update"] = 3] = "Update";
    InstallOperation[InstallOperation["Migrate"] = 4] = "Migrate";
})(InstallOperation || (InstallOperation = {}));
var ExtensionGalleryErrorCode;
(function (ExtensionGalleryErrorCode) {
    ExtensionGalleryErrorCode["Timeout"] = "Timeout";
    ExtensionGalleryErrorCode["Cancelled"] = "Cancelled";
    ExtensionGalleryErrorCode["Failed"] = "Failed";
    ExtensionGalleryErrorCode["DownloadFailedWriting"] = "DownloadFailedWriting";
    ExtensionGalleryErrorCode["Offline"] = "Offline";
})(ExtensionGalleryErrorCode || (ExtensionGalleryErrorCode = {}));
class ExtensionGalleryError extends Error {
    constructor(message, code) {
        super(message);
        this.code = code;
        this.name = code;
    }
}
var ExtensionManagementErrorCode;
(function (ExtensionManagementErrorCode) {
    ExtensionManagementErrorCode["NotFound"] = "NotFound";
    ExtensionManagementErrorCode["Unsupported"] = "Unsupported";
    ExtensionManagementErrorCode["Deprecated"] = "Deprecated";
    ExtensionManagementErrorCode["Malicious"] = "Malicious";
    ExtensionManagementErrorCode["Incompatible"] = "Incompatible";
    ExtensionManagementErrorCode["IncompatibleApi"] = "IncompatibleApi";
    ExtensionManagementErrorCode["IncompatibleTargetPlatform"] = "IncompatibleTargetPlatform";
    ExtensionManagementErrorCode["ReleaseVersionNotFound"] = "ReleaseVersionNotFound";
    ExtensionManagementErrorCode["Invalid"] = "Invalid";
    ExtensionManagementErrorCode["Download"] = "Download";
    ExtensionManagementErrorCode["DownloadSignature"] = "DownloadSignature";
    ExtensionManagementErrorCode["DownloadFailedWriting"] = "DownloadFailedWriting";
    ExtensionManagementErrorCode["UpdateMetadata"] = "UpdateMetadata";
    ExtensionManagementErrorCode["Extract"] = "Extract";
    ExtensionManagementErrorCode["Scanning"] = "Scanning";
    ExtensionManagementErrorCode["ScanningExtension"] = "ScanningExtension";
    ExtensionManagementErrorCode["ReadRemoved"] = "ReadRemoved";
    ExtensionManagementErrorCode["UnsetRemoved"] = "UnsetRemoved";
    ExtensionManagementErrorCode["Delete"] = "Delete";
    ExtensionManagementErrorCode["Rename"] = "Rename";
    ExtensionManagementErrorCode["IntializeDefaultProfile"] = "IntializeDefaultProfile";
    ExtensionManagementErrorCode["AddToProfile"] = "AddToProfile";
    ExtensionManagementErrorCode["InstalledExtensionNotFound"] = "InstalledExtensionNotFound";
    ExtensionManagementErrorCode["PostInstall"] = "PostInstall";
    ExtensionManagementErrorCode["CorruptZip"] = "CorruptZip";
    ExtensionManagementErrorCode["IncompleteZip"] = "IncompleteZip";
    ExtensionManagementErrorCode["PackageNotSigned"] = "PackageNotSigned";
    ExtensionManagementErrorCode["SignatureVerificationInternal"] = "SignatureVerificationInternal";
    ExtensionManagementErrorCode["SignatureVerificationFailed"] = "SignatureVerificationFailed";
    ExtensionManagementErrorCode["NotAllowed"] = "NotAllowed";
    ExtensionManagementErrorCode["Gallery"] = "Gallery";
    ExtensionManagementErrorCode["Cancelled"] = "Cancelled";
    ExtensionManagementErrorCode["Unknown"] = "Unknown";
    ExtensionManagementErrorCode["Internal"] = "Internal";
})(ExtensionManagementErrorCode || (ExtensionManagementErrorCode = {}));
var ExtensionSignatureVerificationCode;
(function (ExtensionSignatureVerificationCode) {
    ExtensionSignatureVerificationCode["NotSigned"] = "NotSigned";
    ExtensionSignatureVerificationCode["Success"] = "Success";
    ExtensionSignatureVerificationCode["RequiredArgumentMissing"] = "RequiredArgumentMissing";
    ExtensionSignatureVerificationCode["InvalidArgument"] = "InvalidArgument";
    ExtensionSignatureVerificationCode["PackageIsUnreadable"] = "PackageIsUnreadable";
    ExtensionSignatureVerificationCode["UnhandledException"] = "UnhandledException";
    ExtensionSignatureVerificationCode["SignatureManifestIsMissing"] = "SignatureManifestIsMissing";
    ExtensionSignatureVerificationCode["SignatureManifestIsUnreadable"] = "SignatureManifestIsUnreadable";
    ExtensionSignatureVerificationCode["SignatureIsMissing"] = "SignatureIsMissing";
    ExtensionSignatureVerificationCode["SignatureIsUnreadable"] = "SignatureIsUnreadable";
    ExtensionSignatureVerificationCode["CertificateIsUnreadable"] = "CertificateIsUnreadable";
    ExtensionSignatureVerificationCode["SignatureArchiveIsUnreadable"] = "SignatureArchiveIsUnreadable";
    ExtensionSignatureVerificationCode["FileAlreadyExists"] = "FileAlreadyExists";
    ExtensionSignatureVerificationCode["SignatureArchiveIsInvalidZip"] = "SignatureArchiveIsInvalidZip";
    ExtensionSignatureVerificationCode["SignatureArchiveHasSameSignatureFile"] = "SignatureArchiveHasSameSignatureFile";
    ExtensionSignatureVerificationCode["PackageIntegrityCheckFailed"] = "PackageIntegrityCheckFailed";
    ExtensionSignatureVerificationCode["SignatureIsInvalid"] = "SignatureIsInvalid";
    ExtensionSignatureVerificationCode["SignatureManifestIsInvalid"] = "SignatureManifestIsInvalid";
    ExtensionSignatureVerificationCode["SignatureIntegrityCheckFailed"] = "SignatureIntegrityCheckFailed";
    ExtensionSignatureVerificationCode["EntryIsMissing"] = "EntryIsMissing";
    ExtensionSignatureVerificationCode["EntryIsTampered"] = "EntryIsTampered";
    ExtensionSignatureVerificationCode["Untrusted"] = "Untrusted";
    ExtensionSignatureVerificationCode["CertificateRevoked"] = "CertificateRevoked";
    ExtensionSignatureVerificationCode["SignatureIsNotValid"] = "SignatureIsNotValid";
    ExtensionSignatureVerificationCode["UnknownError"] = "UnknownError";
    ExtensionSignatureVerificationCode["PackageIsInvalidZip"] = "PackageIsInvalidZip";
    ExtensionSignatureVerificationCode["SignatureArchiveHasTooManyEntries"] = "SignatureArchiveHasTooManyEntries";
})(ExtensionSignatureVerificationCode || (ExtensionSignatureVerificationCode = {}));
class ExtensionManagementError extends Error {
    constructor(message, code) {
        super(message);
        this.code = code;
        this.name = code;
    }
}
const DISABLED_EXTENSIONS_STORAGE_PATH = 'extensionsIdentifiers/disabled';
const ENABLED_EXTENSIONS_STORAGE_PATH = 'extensionsIdentifiers/enabled';
async function computeSize(location, fileService) {
    let stat;
    try {
        stat = await fileService.resolve(location);
    }
    catch (e) {
        if (e.fileOperationResult === FileOperationResult.FILE_NOT_FOUND) {
            return 0;
        }
        throw e;
    }
    if (stat.children) {
        const sizes = await Promise.all(( stat.children.map(c => computeSize(c.resource, fileService))));
        return sizes.reduce((r, s) => r + s, 0);
    }
    return stat.size ?? 0;
}
const ExtensionsLocalizedLabel = ( localize2(1769, "Extensions"));
const PreferencesLocalizedLabel = ( localize2(1770, 'Preferences'));
const UseUnpkgResourceApiConfigKey = 'extensions.gallery.useUnpkgResourceApi';
const AllowedExtensionsConfigKey = 'extensions.allowed';

export { AllowedExtensionsConfigKey, DISABLED_EXTENSIONS_STORAGE_PATH, ENABLED_EXTENSIONS_STORAGE_PATH, EXTENSION_IDENTIFIER_PATTERN, EXTENSION_IDENTIFIER_REGEX, EXTENSION_INSTALL_DEP_PACK_CONTEXT, EXTENSION_INSTALL_SKIP_PUBLISHER_TRUST_CONTEXT, EXTENSION_INSTALL_SKIP_WALKTHROUGH_CONTEXT, EXTENSION_INSTALL_SOURCE_CONTEXT, ExtensionGalleryError, ExtensionGalleryErrorCode, ExtensionInstallSource, ExtensionManagementError, ExtensionManagementErrorCode, ExtensionSignatureVerificationCode, ExtensionsLocalizedLabel, FilterType, InstallOperation, PreferencesLocalizedLabel, SortBy, SortOrder, StatisticType, TargetPlatformToString, UseUnpkgResourceApiConfigKey, WEB_EXTENSION_TAG, computeSize, getTargetPlatform, isIExtensionIdentifier, isNotWebExtensionInWebTargetPlatform, isTargetPlatformCompatible, toTargetPlatform };
