/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { CancellationToken } from '@sussudio/base/common/cancellation.mjs';
import { Emitter, Event } from '@sussudio/base/common/event.mjs';
import { Disposable } from '@sussudio/base/common/lifecycle.mjs';
import { URI } from '@sussudio/base/common/uri.mjs';
import {
	IExtensionGalleryService,
	IExtensionIdentifier,
	IExtensionManagementParticipant,
	IGalleryExtension,
	IGalleryMetadata,
	ILocalExtension,
	InstallOperation,
	IExtensionsControlManifest,
	InstallOptions,
	InstallVSIXOptions,
	UninstallOptions,
	Metadata,
	InstallExtensionEvent,
	DidUninstallExtensionEvent,
	InstallExtensionResult,
	UninstallExtensionEvent,
	IExtensionManagementService,
} from './extensionManagement.mjs';
import { ExtensionType, IExtensionManifest, TargetPlatform } from '../../extensions/common/extensions.mjs';
import { ILogService } from '../../log/common/log.mjs';
import { IProductService } from '../../product/common/productService.mjs';
import { ITelemetryService } from '../../telemetry/common/telemetry.mjs';
import { IUserDataProfilesService } from '../../userDataProfile/common/userDataProfile.mjs';
export declare const enum ExtensionVerificationStatus {
	'Verified' = 'Verified',
	'Unverified' = 'Unverified',
	'UnknownError' = 'UnknownError',
}
export type InstallExtensionTaskOptions = InstallOptions &
	InstallVSIXOptions & {
		readonly profileLocation: URI;
	};
export interface IInstallExtensionTask {
	readonly identifier: IExtensionIdentifier;
	readonly source: IGalleryExtension | URI;
	readonly operation: InstallOperation;
	readonly verificationStatus?: ExtensionVerificationStatus;
	run(): Promise<{
		local: ILocalExtension;
		metadata: Metadata;
	}>;
	waitUntilTaskIsFinished(): Promise<{
		local: ILocalExtension;
		metadata: Metadata;
	}>;
	cancel(): void;
}
export type UninstallExtensionTaskOptions = UninstallOptions & {
	readonly profileLocation: URI;
};
export interface IUninstallExtensionTask {
	readonly extension: ILocalExtension;
	run(): Promise<void>;
	waitUntilTaskIsFinished(): Promise<void>;
	cancel(): void;
}
export declare abstract class AbstractExtensionManagementService
	extends Disposable
	implements IExtensionManagementService
{
	protected readonly galleryService: IExtensionGalleryService;
	protected readonly telemetryService: ITelemetryService;
	protected readonly logService: ILogService;
	protected readonly productService: IProductService;
	protected readonly userDataProfilesService: IUserDataProfilesService;
	readonly _serviceBrand: undefined;
	private extensionsControlManifest;
	private lastReportTimestamp;
	private readonly installingExtensions;
	private readonly uninstallingExtensions;
	private readonly _onInstallExtension;
	get onInstallExtension(): Event<InstallExtensionEvent>;
	protected readonly _onDidInstallExtensions: Emitter<InstallExtensionResult[]>;
	get onDidInstallExtensions(): Event<InstallExtensionResult[]>;
	protected readonly _onUninstallExtension: Emitter<UninstallExtensionEvent>;
	get onUninstallExtension(): Event<UninstallExtensionEvent>;
	protected _onDidUninstallExtension: Emitter<DidUninstallExtensionEvent>;
	get onDidUninstallExtension(): Event<DidUninstallExtensionEvent>;
	private readonly participants;
	constructor(
		galleryService: IExtensionGalleryService,
		telemetryService: ITelemetryService,
		logService: ILogService,
		productService: IProductService,
		userDataProfilesService: IUserDataProfilesService,
	);
	canInstall(extension: IGalleryExtension): Promise<boolean>;
	installFromGallery(extension: IGalleryExtension, options?: InstallOptions): Promise<ILocalExtension>;
	uninstall(extension: ILocalExtension, options?: UninstallOptions): Promise<void>;
	getExtensionsControlManifest(): Promise<IExtensionsControlManifest>;
	registerParticipant(participant: IExtensionManagementParticipant): void;
	protected installExtension(
		manifest: IExtensionManifest,
		extension: URI | IGalleryExtension,
		options: InstallOptions & InstallVSIXOptions,
	): Promise<ILocalExtension>;
	private canWaitForTask;
	private joinAllSettled;
	private getAllDepsAndPackExtensions;
	private checkAndGetCompatibleVersion;
	protected getCompatibleVersion(
		extension: IGalleryExtension,
		sameVersion: boolean,
		includePreRelease: boolean,
	): Promise<IGalleryExtension | null>;
	private uninstallExtension;
	private checkForDependents;
	private getDependentsErrorMessage;
	private getAllPackExtensionsToUninstall;
	private getDependents;
	private updateControlCache;
	abstract getTargetPlatform(): Promise<TargetPlatform>;
	abstract zip(extension: ILocalExtension): Promise<URI>;
	abstract unzip(zipLocation: URI): Promise<IExtensionIdentifier>;
	abstract getManifest(vsix: URI): Promise<IExtensionManifest>;
	abstract install(vsix: URI, options?: InstallVSIXOptions): Promise<ILocalExtension>;
	abstract installFromLocation(location: URI, profileLocation: URI): Promise<ILocalExtension>;
	abstract getInstalled(type?: ExtensionType, profileLocation?: URI): Promise<ILocalExtension[]>;
	abstract download(extension: IGalleryExtension, operation: InstallOperation): Promise<URI>;
	abstract reinstallFromGallery(extension: ILocalExtension): Promise<ILocalExtension>;
	abstract getMetadata(extension: ILocalExtension): Promise<Metadata | undefined>;
	abstract updateMetadata(local: ILocalExtension, metadata: IGalleryMetadata): Promise<ILocalExtension>;
	abstract updateExtensionScope(local: ILocalExtension, isMachineScoped: boolean): Promise<ILocalExtension>;
	protected abstract getCurrentExtensionsManifestLocation(): URI;
	protected abstract createInstallExtensionTask(
		manifest: IExtensionManifest,
		extension: URI | IGalleryExtension,
		options: InstallExtensionTaskOptions,
	): IInstallExtensionTask;
	protected abstract createUninstallExtensionTask(
		extension: ILocalExtension,
		options: UninstallExtensionTaskOptions,
	): IUninstallExtensionTask;
}
export declare function joinErrors(errorOrErrors: (Error | string) | Array<Error | string>): Error;
export declare function reportTelemetry(
	telemetryService: ITelemetryService,
	eventName: string,
	{
		extensionData,
		verificationStatus,
		duration,
		error,
		durationSinceUpdate,
	}: {
		extensionData: any;
		verificationStatus?: ExtensionVerificationStatus;
		duration?: number;
		durationSinceUpdate?: number;
		error?: Error;
	},
): void;
export declare abstract class AbstractExtensionTask<T> {
	private readonly barrier;
	private cancellablePromise;
	waitUntilTaskIsFinished(): Promise<T>;
	run(): Promise<T>;
	cancel(): void;
	protected abstract doRun(token: CancellationToken): Promise<T>;
}
