"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 = apiGithubLogin;
const handleSocialDb_1 = __importDefault(require("../../social-login/handleSocialDb"));
const githubLogin_1 = __importDefault(require("../../social-login/githubLogin"));
const camelJoinedtoCamelSpace_1 = __importDefault(require("../../../../utils/camelJoinedtoCamelSpace"));
/**
 * # API Login with Github
 */
function apiGithubLogin(_a) {
    return __awaiter(this, arguments, void 0, function* ({ code, clientId, clientSecret, database, additionalFields, email, additionalData, }) {
        if (!code || !clientId || !clientSecret || !database) {
            return {
                success: false,
                msg: "Missing query params",
            };
        }
        if (typeof code !== "string" ||
            typeof clientId !== "string" ||
            typeof clientSecret !== "string" ||
            typeof database !== "string") {
            return {
                success: false,
                msg: "Wrong Parameters",
            };
        }
        /**
         * Create new user folder and file
         *
         * @description Create new user folder and file
         */
        const gitHubUser = yield (0, githubLogin_1.default)({
            code: code,
            clientId: clientId,
            clientSecret: clientSecret,
        });
        if (!gitHubUser) {
            return {
                success: false,
                msg: "No github user returned",
            };
        }
        const socialId = gitHubUser.name || gitHubUser.id || gitHubUser.login;
        const targetName = gitHubUser.name || gitHubUser.login;
        const nameArray = (targetName === null || targetName === void 0 ? void 0 : targetName.match(/ /))
            ? targetName === null || targetName === void 0 ? void 0 : targetName.split(" ")
            : (targetName === null || targetName === void 0 ? void 0 : targetName.match(/\-/))
                ? targetName === null || targetName === void 0 ? void 0 : targetName.split("-")
                : [targetName];
        let payload = {
            email: gitHubUser.email,
            first_name: (0, camelJoinedtoCamelSpace_1.default)(nameArray[0]),
            last_name: (0, camelJoinedtoCamelSpace_1.default)(nameArray[1]),
            social_id: socialId,
            social_platform: "github",
            image: gitHubUser.avatar_url,
            image_thumbnail: gitHubUser.avatar_url,
            username: "github-user-" + socialId,
        };
        if (additionalData) {
            payload = Object.assign(Object.assign({}, payload), additionalData);
        }
        const loggedInGithubUser = yield (0, handleSocialDb_1.default)({
            database,
            email: gitHubUser.email,
            payload,
            social_platform: "github",
            social_id: socialId,
            supEmail: email,
            additionalFields,
        });
        ////////////////////////////////////////////////
        ////////////////////////////////////////////////
        ////////////////////////////////////////////////
        return Object.assign({}, loggedInGithubUser);
    });
}