// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { MsalNode, hasNativeBroker } from "./msalNodeCommon";
import { credentialLogger } from "../../util/logging";
import open from "open";
/**
 * A call to open(), but mockable
 * @internal
 */
export const interactiveBrowserMockable = {
    open,
};
/**
 * This MSAL client sets up a web server to listen for redirect callbacks, then calls to the MSAL's public application's `acquireTokenByDeviceCode` during `doGetToken`
 * to trigger the authentication flow, and then respond based on the values obtained from the redirect callback
 * @internal
 */
export class MsalOpenBrowser extends MsalNode {
    constructor(options) {
        var _a, _b;
        super(options);
        this.loginHint = options.loginHint;
        this.errorTemplate = (_a = options.browserCustomizationOptions) === null || _a === void 0 ? void 0 : _a.errorMessage;
        this.successTemplate = (_b = options.browserCustomizationOptions) === null || _b === void 0 ? void 0 : _b.successMessage;
        this.logger = credentialLogger("Node.js MSAL Open Browser");
    }
    async doGetToken(scopes, options) {
        var _a;
        try {
            const interactiveRequest = {
                openBrowser: async (url) => {
                    await interactiveBrowserMockable.open(url, { wait: true, newInstance: true });
                },
                scopes,
                authority: options === null || options === void 0 ? void 0 : options.authority,
                claims: options === null || options === void 0 ? void 0 : options.claims,
                correlationId: options === null || options === void 0 ? void 0 : options.correlationId,
                loginHint: this.loginHint,
                errorTemplate: this.errorTemplate,
                successTemplate: this.successTemplate,
            };
            if (hasNativeBroker() && this.enableBroker) {
                this.logger.verbose("Authentication will resume through the broker");
                if (this.parentWindowHandle) {
                    interactiveRequest.windowHandle = Buffer.from(this.parentWindowHandle);
                }
                else {
                    // error should have been thrown from within the constructor of InteractiveBrowserCredential
                    this.logger.warning("Parent window handle is not specified for the broker. This may cause unexpected behavior. Please provide the parentWindowHandle.");
                }
                if (this.enableMsaPassthrough) {
                    ((_a = interactiveRequest.tokenQueryParameters) !== null && _a !== void 0 ? _a : (interactiveRequest.tokenQueryParameters = {}))["msal_request_type"] =
                        "consumer_passthrough";
                }
            }
            if (hasNativeBroker() && !this.enableBroker) {
                this.logger.verbose("Authentication will resume normally without the broker, since it's not enabled");
            }
            const result = await this.getApp("public", options === null || options === void 0 ? void 0 : options.enableCae).acquireTokenInteractive(interactiveRequest);
            if (result.fromNativeBroker) {
                this.logger.verbose(`This result is returned from native broker`);
            }
            return this.handleResult(scopes, this.clientId, result || undefined);
        }
        catch (err) {
            throw this.handleError(scopes, err, options);
        }
    }
}
//# sourceMappingURL=msalOpenBrowser.js.map