"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendOtpWhatsapp = void 0;
const tslib_1 = require("tslib");
// import axios from "axios";
// export async function sendOtpWhatsapp(phoneNumber: string, otp: string): Promise<{success: boolean; isWhatsapp?: boolean}> {
//   try {
//     const apiKey = "645aa044d4444923913d976d0038374d";
//     const message = `Your OTP is ${otp}`;
//     const response = await axios.get("http://148.251.129.118/wapp/api/send", {
//       params: {apikey: apiKey, mobile: phoneNumber, msg: message}
//     });
//     console.log("WhatsApp API Response:", JSON.stringify(response.data));
//     const responseData = response.data?.toString().toLowerCase();
//     console.log(responseData)
//     if (response.data?.toString().includes("Invalid") || response.data?.toString().includes("not whatsapp")) {
//       return {success: false, isWhatsapp: false};
//     }
//     return {success: true, isWhatsapp: true};
//   } catch (error: any) {
//     console.error("WhatsApp API Error Response Data:", error.response?.data);
//     console.error("WhatsApp API Error Message:", error.message);
//     if (error.response?.data?.toString().includes("Invalid")) {
//       return {success: false, isWhatsapp: false};
//     }
//     return {success: false};
//   }
// }
// import axios from 'axios';
// export interface WhatsappOtpResponse {
//   success: boolean;
//   isWhatsapp?: boolean;
//   error?: string;
// }
// export async function sendOtpWhatsapp(
//   phoneNumber: string,
//   otp: string
// ): Promise<WhatsappOtpResponse> {
//   try {
//     const apiKey = 'bXZzR25YWnpOaEM2dGdXWG9DV3pmdz09';
//     const message = `Your OTP is ${otp}`;
//     const response = await axios.get('http://148.251.129.118/wapp/api/send', {
//       params: {apikey: apiKey, mobile: phoneNumber, msg: message},
//     });
//     console.log('WhatsApp API Response:', JSON.stringify(response.data));
//     const responseData = response.data;
//     if (
//       responseData?.status === 'ERROR' &&
//       responseData?.errormsg?.toLowerCase().includes('no valid mobile found')
//     ) {
//       return {success: false, isWhatsapp: false, error: responseData.errormsg};
//     }
//     return {success: true, isWhatsapp: true};
//   } catch (error: any) {
//     console.error('WhatsApp API Error Response Data:', error.response?.data);
//     console.error('WhatsApp API Error Message:', error.message);
//     const errorData = error.response?.data?.errormsg?.toString().toLowerCase() || '';
//     if (errorData.includes('no valid mobile found')) {
//       return {success: false, isWhatsapp: false, error: error.response?.data?.errormsg};
//     }
//     return {success: false, error: error.message};
//   }
// };
const axios_1 = tslib_1.__importDefault(require("axios"));
async function sendOtpWhatsapp(phoneNumber, otp) {
    var _a, _b, _c, _d, _e;
    try {
        const token = 'bXZzR25YWnpOaEM2dGdXWG9DV3pmdz09';
        const username = 'Comtranse';
        // ⚠️ Phone must be with country code and NO +
        const formattedNumber = phoneNumber.startsWith('91')
            ? phoneNumber
            : `91${phoneNumber}`;
        const message = `Your OTP is ${otp}`;
        const response = await axios_1.default.get('https://int.chatway.in/api/send-msg', {
            params: {
                username,
                number: formattedNumber,
                message,
                token,
            },
        });
        // console.log('Chatway WhatsApp Response:', response.data);
        // Adjust success condition based on actual API response
        if (((_a = response.data) === null || _a === void 0 ? void 0 : _a.status) === true || response.status === 200) {
            return { success: true, isWhatsapp: true };
        }
        return {
            success: false,
            isWhatsapp: false,
            error: ((_b = response.data) === null || _b === void 0 ? void 0 : _b.message) || 'Unknown API error',
        };
    }
    catch (error) {
        console.error('Chatway API Error:', ((_c = error.response) === null || _c === void 0 ? void 0 : _c.data) || error.message);
        return {
            success: false,
            isWhatsapp: false,
            error: ((_e = (_d = error.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.message) || error.message,
        };
    }
}
exports.sendOtpWhatsapp = sendOtpWhatsapp;
//# sourceMappingURL=whatsapp.service.js.map