"use strict";
/* ---------------------------
   Bind Button
----------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadPdf = downloadPdf;
exports.downloadExcel = downloadExcel;
const plugins_1 = require("../plugins");
function downloadPdf(op) {
    try {
        const btn = op?.btn;
        if (typeof btn !== 'string' || btn.trim() === '') {
            throw new Error('Invalid "btn". Expected non-empty string');
        }
        const $el = $('#' + btn); // allow any selector
        if ($el.length === 0) {
            throw new Error(`Element not found for selector: ${btn}`);
        }
        $el.off('click') // namespaced
            .on('click', async function () {
            try {
                const attr = $(this).attr('data-pdf-op');
                if (!attr) {
                    throw new Error(`Missing data-pdf-op on ${btn}`);
                }
                let extra = {};
                try {
                    extra = JSON.parse(attr);
                }
                catch {
                    throw new Error(`Invalid JSON in data-pdf-op: ${attr}`);
                }
                const options = { ...op, ...extra };
                await (0, plugins_1.MakePdf)(options);
            }
            catch (err) {
                console.error('PDF Click Handler', err);
            }
        });
    }
    catch (e) {
        console.error('PDF Initialization', e);
    }
}
/* -----------------------------
   Excel Button Handler
----------------------------- */
function downloadExcel(op = {}) {
    const btn = op.btn ?? 'excelDownload';
    const $btn = $('#' + btn);
    $btn.off('click');
    $btn.on('click', async function () {
        const dataset = $(this).attr('data-excel-op');
        const parsed = dataset ? JSON.parse(dataset) : {};
        const newOp = {
            ...op,
            ...parsed,
        };
        await (0, plugins_1.makeExcel)(newOp);
    });
}
//# sourceMappingURL=download.js.map