/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { ErrorCodes, getErrorMessage, PowerDataRuntimeError } from '../error/error';
/**
 * Error types specific to RuntimeDataSourceService
 * @private
 */
export var DataSourceServiceError;
(function (DataSourceServiceError) {
})(DataSourceServiceError || (DataSourceServiceError = {}));
/**
 * RuntimeDataSourceService provides functionality for managing and retrieving
 * data source information.
 * @private
 */
export class RuntimeDataSourceService {
    _powerDataSourcesInfoProvider;
    /**
     * Data source information
     */
    _dataSourcesInfo;
    /**
     * Indicates whether the service has been initialized
     */
    _isInitialized;
    /**
     * Creates a new instance of RuntimeDataSourceService
     */
    constructor(_powerDataSourcesInfoProvider) {
        this._powerDataSourcesInfoProvider = _powerDataSourcesInfoProvider;
        this._dataSourcesInfo = {};
        this._isInitialized = false;
    }
    /**
     * Initializes the service by loading user data sources
     * @throws PowerDataRuntimeError if initialization fails
     */
    async initialize() {
        try {
            const userDataSources = await this._getUserDataSources();
            this._dataSourcesInfo = {};
            Object.keys(userDataSources).forEach((key) => {
                this._dataSourcesInfo[key] = userDataSources[key];
            });
            this._isInitialized = true;
        }
        catch (error) {
            throw new PowerDataRuntimeError(ErrorCodes.InitializationError, getErrorMessage(error));
        }
    }
    /**
     * Gets all user data sources
     * @returns Array of data source information
     * @throws PowerDataRuntimeError if service is not initialized
     */
    async getUserDataSources() {
        await this._ensureInitialized();
        return this._dataSourcesInfo;
    }
    /**
     * Gets information for a specific data source
     * @param dataSource - The ID of the data source
     * @returns Data source information
     * @throws PowerDataRuntimeError if data source is not found or service is not initialized
     */
    async getDataSource(dataSource) {
        await this._ensureInitialized();
        const dataSourceInfo = this._dataSourcesInfo[dataSource];
        if (!dataSourceInfo) {
            const errorMessage = `Unable to find data source: ${dataSource} in data sources info.`;
            throw new PowerDataRuntimeError(ErrorCodes.DataSourceNotFound, errorMessage);
        }
        return dataSourceInfo;
    }
    /**
     * Checks if a data source exists
     * @param dataSourceId - The ID of the data source to check
     * @returns True if the data source exists, false otherwise
     * @throws PowerDataRuntimeError if service is not initialized
     */
    async hasDataSource(dataSource) {
        await this._ensureInitialized();
        return dataSource in this._dataSourcesInfo;
    }
    /**
     * Ensures the service is initialized
     * @throws PowerDataRuntimeError if service is not initialized
     */
    async _ensureInitialized() {
        if (!this._isInitialized) {
            await this.initialize();
        }
    }
    /**
     * Gets user data sources from the provided data source schemas
     * @returns Promise resolving to array of data source information
     */
    async _getUserDataSources() {
        const dataSourcesInfo = await this._powerDataSourcesInfoProvider.getDataSourcesInfo();
        return Promise.resolve(dataSourcesInfo);
    }
}
//# sourceMappingURL=runtimeDataSourceService.js.map