"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getAccessToken;
exports.googleLogin = googleLogin;
let interval;
/**
 * Login with Google Function
 * ===============================================================================
 * @description This function uses google identity api to login a user with datasquirel
 */
function getAccessToken(params) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b;
        (_a = params.setLoading) === null || _a === void 0 ? void 0 : _a.call(params, true);
        const response = (yield new Promise((resolve, reject) => {
            interval = setInterval(() => {
                // @ts-ignore
                let google = window.google;
                if (google) {
                    window.clearInterval(interval);
                    resolve(googleLogin(Object.assign(Object.assign({}, params), { google })));
                }
            }, 500);
        }));
        (_b = params.setLoading) === null || _b === void 0 ? void 0 : _b.call(params, false);
        return response;
    });
}
/**
 * # Google Login Function
 */
function googleLogin({ google, clientId, setLoading, triggerPrompt, }) {
    setTimeout(() => {
        setLoading === null || setLoading === void 0 ? void 0 : setLoading(false);
    }, 3000);
    return new Promise((resolve, reject) => {
        /**
         * # Callback Function
         * @param {import("../../../package-shared/types").GoogleAccessTokenObject} response
         */
        function handleCredentialResponse(response) {
            resolve(response.access_token);
        }
        const googleAuth = google.accounts.oauth2.initTokenClient({
            client_id: clientId,
            scope: "email profile",
            callback: handleCredentialResponse,
        });
        googleAuth.requestAccessToken();
        if (triggerPrompt) {
            google.accounts.id.prompt(triggerGooglePromptCallback);
        }
        /**
         * Google prompt notification callback
         * ========================================================
         * @param {import("../../../package-shared/types").GoogleIdentityPromptNotification} notification
         */
        function triggerGooglePromptCallback(notification) {
            console.log(notification);
        }
    });
}