/*!
 * Copyright (C) Microsoft Corporation. All rights reserved.
 */
import { ErrorCodes, getErrorMessage, PowerDataRuntimeError } from '../error/error';
import { RuntimeDataClient } from './runtimeDataClient';
import { RuntimeMetadataClient } from './runtimeMetadataClient';
/**
 * RuntimeClientProvider manages the lifecycle of runtime clients for data and metadata operations.
 * Implements a lazy initialization pattern for clients and ensures proper error handling.
 * @private
 */
export class RuntimeClientProvider {
    // Private members for data and metadata clients
    // The data client is responsible for handling data operations
    _dataClient;
    // The metadata client is responsible for handling metadata operations
    _metadataClient;
    // The operation executor is used to execute operations on the clients
    // It is an instance of IPowerOperationExecutor, which provides the necessary methods for executing operations
    _operationExecutor;
    // Constructor for RuntimeClientProvider
    // Accepts an optional IPowerOperationExecutor instance for executing operations
    // If not provided, uses the default PowerOperationExecutor instance
    constructor(powerOperationExecutor) {
        this._operationExecutor = powerOperationExecutor;
    }
    /**
     * Gets or initializes the data client
     * @throws Error if client initialization fails
     * @returns Promise resolving to IRuntimeDataClient
     */
    async getDataClientAsync() {
        try {
            if (!this._dataClient) {
                this._dataClient = await this._initializeDataClient();
            }
            if (!this._dataClient) {
                throw new PowerDataRuntimeError(ErrorCodes.DataClientNotInitialized);
            }
            return this._dataClient;
        }
        catch (error) {
            throw new PowerDataRuntimeError(ErrorCodes.DataClientInitFailed, getErrorMessage(error));
        }
    }
    /**
     * Gets or initializes the metadata client
     * @throws Error if client initialization fails
     * @returns Promise resolving to IRuntimeMetadataClient
     */
    async getMetadataClientAsync() {
        try {
            if (!this._metadataClient) {
                this._metadataClient = await this._initializeMetadataClient();
            }
            if (!this._metadataClient) {
                throw new PowerDataRuntimeError(ErrorCodes.MetadataClientNotInitialized);
            }
            return this._metadataClient;
        }
        catch (error) {
            throw new PowerDataRuntimeError(ErrorCodes.MetadataClientInitFailed, getErrorMessage(error));
        }
    }
    /**
     * Initializes the data client
     * @returns Promise resolving to IRuntimeDataClient
     */
    async _initializeDataClient() {
        return RuntimeDataClient.createInstanceAsync(this._operationExecutor);
    }
    /**
     * Initializes the metadata client
     * @returns Promise resolving to IRuntimeMetadataClient
     */
    async _initializeMetadataClient() {
        return RuntimeMetadataClient.createInstanceAsync(this._operationExecutor);
    }
    /**
     * Resets both clients, forcing re-initialization on next use
     * Useful for testing or recovering from error states
     */
    reset() {
        this._dataClient = undefined;
        this._metadataClient = undefined;
    }
}
//# sourceMappingURL=runtimeClientProvider.js.map