import { Events } from '../../gc-core-assets/lib/Events';
import { GcConsole } from '../../gc-core-assets/lib/GcConsole';
import { GcFiles } from '../../gc-core-assets/lib/GcFiles';
import { GcUtils } from '../../gc-core-assets/lib/GcUtils';
import { ServiceType, ServicesRegistry } from '../../gc-core-services/lib/ServicesRegistry';
import { backplaneServiceType } from '../../gc-service-backplane/lib/BackplaneService';

/**
 *  Copyright (c) 2025 Texas Instruments Incorporated
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  *   Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  *   Neither the name of Texas Instruments Incorporated nor the names of
 *  its contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
const MODULE_NAME = 'gc-service-tirex';
const console = new GcConsole(MODULE_NAME);
const tirexServiceType = new ServiceType(MODULE_NAME);
/**
 * Tirex service.
 * @hidden
 */
class TirexService extends Events {
    async ensureTirexModule() {
        if (!this.tirexService) {
            const backplaneService = ServicesRegistry.getService(backplaneServiceType);
            this.tirexService = await backplaneService.getSubModule('Tirex');
            await this.tirexService.init({ ccsPort: 0, proxy: '', isTheia: false });
        }
    }
    addEventListener(type, listener) {
        this.ensureTirexModule().then(() => {
            this.tirexService.addListener(type.eventName, listener);
        });
    }
    async refreshInstalledPackageInformation(packageId, version, additionalDependencies) {
        const packageInformation = await this.tirexService.getInstallInfoForPackageDependencies({
            packageId: packageId,
            version: version,
            additionalDependencies: additionalDependencies
        }, { excludePackageAsDependency: false });
        const installedPackageInformation = packageInformation.filter((packageInfo) => packageInfo.installInfoType === 'installed').map((packageInfo) => ({
            'packagePublicId': packageInfo.packageId,
            'packageVersion': packageInfo.packageVersion,
            'installLocation': packageInfo.installLocation
        }));
        this.writeInstalledPackageInformationToFile(installedPackageInformation);
    }
    async writeInstalledPackageInformationToFile(installedPackagesInfo) {
        if (GcUtils.isInPreview) {
            GcFiles.writeJsonFile('installedPackagesInfo.json', installedPackagesInfo);
        }
        else {
            const response = await fetch(`/packageInstaller/installLocation/${encodeURIComponent('installedPackagesInfo.json')}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(installedPackagesInfo)
            });
            if (!response.ok) {
                throw new Error('Failed to write to installedPackagesInfo.json');
            }
        }
    }
    async getPackageInformation() {
        await this.ensureTirexModule();
        let projectJson;
        if (GcUtils.isInDesigner || GcUtils.isInPreview) // for development only.
            projectJson = await GcFiles.readJsonFile('project.json');
        else {
            projectJson = await fetch('packageInstaller/project.json');
            if (!projectJson.ok)
                throw new Error('No project.json found, check your launcher.json file.');
            else
                projectJson = await projectJson.json();
        }
        return {
            packageId: projectJson.packageId,
            packageVersion: projectJson.packageVersion,
            packageDependencies: projectJson.dependencies
        };
    }
    removeEventListener(type, listener) {
        this.ensureTirexModule().then(() => {
            this.tirexService.removeListener(type.eventName, listener);
        });
    }
}
ServicesRegistry.register(tirexServiceType, TirexService);

export { MODULE_NAME, TirexService, tirexServiceType };
//# sourceMappingURL=TirexService.js.map
