"use strict";

var __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
    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) {
            var value;
            result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
                resolve(value);
            }))).then(fulfilled, rejected);
        }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    }));
};

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.TableExportPlugin = void 0;

const vtable_1 = require("@visactor/vtable"), index_1 = require("./table-export/index");

class TableExportPlugin {
    constructor(pluginOptions) {
        this.id = "table-export-plugin", this.name = "TableExport", this.runTime = [ vtable_1.TABLE_EVENT_TYPE.INITIALIZED ], 
        this.pluginOptions = Object.assign({
            exportOnIdle: !1,
            exportExcelOptions: {
                downloadFile: !0,
                fileName: "export"
            },
            exportCsvOptions: {
                downloadFile: !0,
                fileName: "export"
            }
        }, pluginOptions);
    }
    run(...args) {
        if (args[1] === vtable_1.TABLE_EVENT_TYPE.INITIALIZED) {
            args[0];
            this.table = args[2], this.table.exportToCsv = () => {
                const options = this.pluginOptions.exportCsvOptions;
                return options.downloadFile ? (0, index_1.downloadCsv)((0, index_1.exportVTableToCsv)(this.table, options), options.fileName || "export") : (0, 
                index_1.exportVTableToCsv)(this.table, options);
            }, this.table.exportToExcel = () => __awaiter(this, void 0, void 0, (function*() {
                const options = this.pluginOptions.exportExcelOptions;
                return options.downloadFile ? yield (0, index_1.downloadExcel)(yield (0, index_1.exportVTableToExcel)(this.table, options, this.pluginOptions.exportOnIdle), options.fileName || "export") : (0, 
                index_1.exportVTableToExcel)(this.table, options, this.pluginOptions.exportOnIdle);
            })), this.table.__vtableSheet && (this.table.__vtableSheet._exportMutipleTablesToExcel || (this.table.__vtableSheet._exportMutipleTablesToExcel = tables => __awaiter(this, void 0, void 0, (function*() {
                const buffer = yield (0, index_1.exportMultipleVTablesToExcel)(tables, this.pluginOptions.exportExcelOptions);
                yield (0, index_1.downloadExcel)(buffer, this.pluginOptions.exportExcelOptions.fileName || "vtable-sheet-export");
            }))));
        }
    }
    release() {
        this.table && (delete this.table.exportToCsv, delete this.table.exportToExcel, this.table = null);
    }
}

exports.TableExportPlugin = TableExportPlugin;
//# sourceMappingURL=table-export.js.map