"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = logout;
const parseClientCookies_1 = __importDefault(require("../utils/parseClientCookies"));
/**
 * Login with Google Function
 * ===============================================================================
 * @description This function uses google identity api to login a user with datasquirel
 */
function logout(params) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            const localUser = localStorage.getItem("user");
            let targetUser;
            try {
                targetUser = JSON.parse(localUser || "");
            }
            catch (error) {
                console.log(error);
            }
            if (!targetUser) {
                return false;
            }
            ////////////////////////////////////////
            ////////////////////////////////////////
            ////////////////////////////////////////
            const cookies = (0, parseClientCookies_1.default)();
            const socialId = (cookies === null || cookies === void 0 ? void 0 : cookies.datasquirel_social_id) &&
                typeof cookies.datasquirel_social_id == "string" &&
                !cookies.datasquirel_social_id.match(/^null$/i)
                ? cookies.datasquirel_social_id
                : null;
            ////////////////////////////////////////
            ////////////////////////////////////////
            ////////////////////////////////////////
            localStorage.setItem("user", "{}");
            localStorage.removeItem("csrf");
            document.cookie = `datasquirel_social_id=null;samesite=strict;path=/`;
            ////////////////////////////////////////
            ////////////////////////////////////////
            ////////////////////////////////////////
            const response = yield new Promise((resolve, reject) => {
                if (socialId && !(socialId === null || socialId === void 0 ? void 0 : socialId.match(/^null$/i))) {
                    const googleClientId = params === null || params === void 0 ? void 0 : params.googleClientId;
                    if (googleClientId) {
                        const googleScript = document.createElement("script");
                        googleScript.src = "https://accounts.google.com/gsi/client";
                        googleScript.className = "social-script-tag";
                        document.body.appendChild(googleScript);
                        ////////////////////////////////////////
                        ////////////////////////////////////////
                        ////////////////////////////////////////
                        googleScript.onload = function (e) {
                            // @ts-ignore
                            const google = window.google;
                            if (google) {
                                ////////////////////////////////////////
                                ////////////////////////////////////////
                                ////////////////////////////////////////
                                google.accounts.id.initialize({
                                    client_id: googleClientId,
                                });
                                google.accounts.id.revoke(socialId, (done) => {
                                    console.log(done.error);
                                    resolve(true);
                                });
                                ////////////////////////////////////////
                                ////////////////////////////////////////
                                ////////////////////////////////////////
                            }
                        };
                    }
                    else {
                        resolve(true);
                    }
                    ////////////////////////////////////////
                    ////////////////////////////////////////
                    ////////////////////////////////////////
                }
                else {
                    resolve(true);
                }
            });
            return response;
        }
        catch (error) {
            return false;
        }
    });
}