"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 VERSION = "v24.0";
class WhatsApp {
    constructor(token = "", phone_number_id = "") {
        this.token = token;
        this.phone_number_id = phone_number_id;
        this.url = `https://graph.facebook.com/${VERSION}/${phone_number_id}/messages`;
        this.headers = {
            "Content-Type": "application/json",
            Authorization: `Bearer ${token}`,
        };
    }
    sendMessage(message, recipient_id, recipient_type = "individual", preview_url = true) {
        return __awaiter(this, void 0, void 0, function* () {
            let data = {
                messaging_product: "whatsapp",
                recipient_type: recipient_type,
                to: recipient_id,
                type: "text",
                text: { preview_url: preview_url, body: message },
            };
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    sendTemplate(template, recipient_id, components = [], lang = "en_US") {
        return __awaiter(this, void 0, void 0, function* () {
            let data = {
                messaging_product: "whatsapp",
                to: recipient_id,
                type: "template",
                template: {
                    name: template,
                    language: { code: lang },
                    components: [...components],
                },
            };
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    sendLocation(lat, long, name, address, recipient_id) {
        return __awaiter(this, void 0, void 0, function* () {
            let data = {
                messaging_product: "whatsapp",
                to: recipient_id,
                type: "location",
                location: {
                    latitude: lat,
                    longitude: long,
                    name: name,
                    address: address,
                },
            };
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    sendImage(image, recipient_id, recipient_type = "individual", caption = null, link = true) {
        return __awaiter(this, void 0, void 0, function* () {
            let data;
            if (link) {
                data = {
                    messaging_product: "whatsapp",
                    recipient_type: recipient_type,
                    to: recipient_id,
                    type: "image",
                    image: { link: image, caption: caption },
                };
            }
            else {
                data = {
                    messaging_product: "whatsapp",
                    recipient_type: recipient_type,
                    to: recipient_id,
                    type: "image",
                    image: { id: image, caption: caption },
                };
            }
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    sendAudio(audio, recipient_id, link = true) {
        return __awaiter(this, void 0, void 0, function* () {
            let data;
            if (link) {
                data = {
                    messaging_product: "whatsapp",
                    to: recipient_id,
                    type: "audio",
                    audio: { link: audio },
                };
            }
            else {
                data = {
                    messaging_product: "whatsapp",
                    to: recipient_id,
                    type: "audio",
                    audio: { id: audio },
                };
            }
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    sendVideo(video, recipient_id, caption = null, link = true) {
        return __awaiter(this, void 0, void 0, function* () {
            let data;
            if (link) {
                data = {
                    messaging_product: "whatsapp",
                    to: recipient_id,
                    type: "video",
                    video: { link: video, caption: caption },
                };
            }
            else {
                data = {
                    messaging_product: "whatsapp",
                    to: recipient_id,
                    type: "video",
                    video: { id: video, caption: caption },
                };
            }
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    sendDocument(document, recipient_id, filename = "document", caption = null, link = true) {
        return __awaiter(this, void 0, void 0, function* () {
            let data;
            if (link) {
                data = {
                    messaging_product: "whatsapp",
                    to: recipient_id,
                    type: "document",
                    document: { link: document, caption: caption, filename: filename },
                };
            }
            else {
                data = {
                    messaging_product: "whatsapp",
                    to: recipient_id,
                    type: "document",
                    document: { id: document, caption: caption, filename: filename },
                };
            }
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    createButton(button) {
        // TODO: Investigate
        return {
            type: "list",
            header: { type: "text", text: button["header"] },
            body: { text: button["body"] },
            footer: { text: button["footer"] },
            action: button["action"],
        };
    }
    sendButton(button, recipient_id) {
        return __awaiter(this, void 0, void 0, function* () {
            let data = {
                messaging_product: "whatsapp",
                to: recipient_id,
                type: "interactive",
                interactive: this.createButton(button),
            };
            let r = yield axios_1.default.post(this.url, data, {
                headers: this.headers,
            });
            return r;
        });
    }
    getMedia(id) {
        return __awaiter(this, void 0, void 0, function* () {
            let media_url = `https://graph.facebook.com/${VERSION}/${id}`;
            let r = yield axios_1.default.get(media_url, {
                headers: this.headers,
            });
            return r;
        });
    }
    deleteMedia(id) {
        return __awaiter(this, void 0, void 0, function* () {
            let media_url = `https://graph.facebook.com/${VERSION}/${id}`;
            let r = yield axios_1.default.delete(media_url, {
                headers: this.headers,
            });
            return r;
        });
    }
}
exports.default = WhatsApp;
