"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 });
const axios_1 = __importDefault(require("axios"));
const AppError_1 = __importDefault(require("../../errors/AppError"));
const EndPointQuery_1 = __importDefault(require("../../helpers/EndPointQuery"));
const GetDefaultWhatsApp_1 = __importDefault(require("../../helpers/GetDefaultWhatsApp"));
const wbot_1 = require("../../libs/wbot");
const CheckIsValidContact = (number, ignoreThrow) => __awaiter(void 0, void 0, void 0, function* () {
    const defaultWhatsapp = yield GetDefaultWhatsApp_1.default({
        ignoreNoWhatsappFound: true
    });
    let isValidNumber;
    if (defaultWhatsapp) {
        const wbot_url = yield wbot_1.getWbot(defaultWhatsapp.id);
        let { data } = yield EndPointQuery_1.default(`${wbot_url}/api/validate`, {
            mobile: `${number}`
        });
        if (data === null || data === void 0 ? void 0 : data.isValid) {
            isValidNumber = data;
        }
    }
    try {
        let _status;
        if (!isValidNumber) {
            const { data, status } = yield axios_1.default.post(`${process.env.WHATS_NUMBER_VALIDATOR_URL}/api/validate`, { mobile: number }, {
                headers: {
                    "Content-Type": "application/json"
                }
            });
            isValidNumber = data;
            _status = status;
        }
        if (ignoreThrow)
            return isValidNumber === null || isValidNumber === void 0 ? void 0 : isValidNumber.number;
        console.log('_status: ', _status);
        if (_status && _status == 422)
            throw new AppError_1.default("ERR_NO_WAPP_FOUND");
        if (!isValidNumber || (isValidNumber && !isValidNumber.isValid)) {
            throw new AppError_1.default("invalidNumber");
        }
        if (isValidNumber && (isValidNumber === null || isValidNumber === void 0 ? void 0 : isValidNumber.isValid))
            return isValidNumber.number;
    }
    catch (err) {
        if (err.message === "invalidNumber") {
            throw new AppError_1.default("ERR_WAPP_INVALID_CONTACT");
        }
        throw new AppError_1.default("ERR_WAPP_CHECK_CONTACT");
    }
});
exports.default = CheckIsValidContact;
