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());
    }));
};

import { vglobal } from "@visactor/vtable/es/vrender";

const checkGanttDependency = () => {
    try {
        return require.resolve("@visactor/vtable-gantt"), !0;
    } catch (e) {
        return !1;
    }
};

export class ExportGanttPlugin {
    constructor() {
        this.id = "gantt-export-helper", this.name = "Gantt Export Helper", this._gantt = null, 
        checkGanttDependency();
    }
    run(...args) {
        const ganttInstance = args[0];
        ganttInstance && (this._gantt = ganttInstance);
    }
    exportToImage(options = {}) {
        var _a;
        return __awaiter(this, void 0, void 0, (function*() {
            if (!checkGanttDependency()) return;
            if (!this._gantt) return;
            const {fileName: fileName = "gantt-export", type: type = "png", quality: quality = 1, backgroundColor: backgroundColor = "#ffffff", scale: scale = window.devicePixelRatio || 1, download: download = !0} = options;
            try {
                const {tempContainer: tempContainer, clonedGantt: clonedGantt} = yield this.createFullSizeContainer(scale);
                try {
                    yield new Promise((resolve => vglobal.getRequestAnimationFrame()(resolve)));
                    const totalWidth = (clonedGantt.taskListTableInstance.getAllColsWidth() + clonedGantt.getAllDateColsWidth()) * scale, totalHeight = clonedGantt.getAllRowsHeight() * scale, exportCanvas = document.createElement("canvas");
                    exportCanvas.width = totalWidth, exportCanvas.height = totalHeight;
                    const ctx = exportCanvas.getContext("2d");
                    ctx.fillStyle = backgroundColor, ctx.fillRect(0, 0, totalWidth, totalHeight), (null === (_a = clonedGantt.taskListTableInstance) || void 0 === _a ? void 0 : _a.canvas) && ctx.drawImage(clonedGantt.taskListTableInstance.canvas, 0, 0, clonedGantt.taskListTableInstance.getAllColsWidth() * scale, totalHeight);
                    const splitLineWidth = 3 * scale, splitLineX = clonedGantt.taskListTableInstance.getAllColsWidth() * scale;
                    ctx.fillStyle = "rgb(225, 228, 232)", ctx.fillRect(splitLineX - splitLineWidth / 2, 0, splitLineWidth, totalHeight);
                    const sourceX = 4 * scale, sourceWidth = clonedGantt.canvas.width - sourceX;
                    return clonedGantt.canvas && ctx.drawImage(clonedGantt.canvas, sourceX, 0, sourceWidth, clonedGantt.canvas.height, (clonedGantt.taskListTableInstance.getAllColsWidth() + 1.5) * scale, 0, (clonedGantt.getAllDateColsWidth() - 1.5) * scale, totalHeight), 
                    this.finalizeExport(exportCanvas, fileName, type, quality, download);
                } finally {
                    tempContainer.remove(), clonedGantt.release();
                }
            } catch (error) {
                throw new Error(`甘特图导出失败: ${error instanceof Error ? error.message : "未知错误"}`);
            }
        }));
    }
    exportToBase64(options = {}) {
        return __awaiter(this, void 0, void 0, (function*() {
            return this.exportToImage(Object.assign(Object.assign({}, options), {
                download: !1
            }));
        }));
    }
    createFullSizeContainer(scale) {
        var _a, _b, _c;
        return __awaiter(this, void 0, void 0, (function*() {
            if (!this._gantt) throw new Error("ExportGanttPlugin: Gantt instance not available to create container.");
            const tempContainer = document.createElement("div");
            tempContainer.style.position = "fixed", tempContainer.style.left = "-9999px", tempContainer.style.overflow = "hidden", 
            tempContainer.style.width = `${window.innerWidth + 100}px`, tempContainer.style.height = `${window.innerHeight + 100}px`, 
            document.body.appendChild(tempContainer);
            const clonedContainer = document.createElement("div"), totalWidth = this._gantt.taskListTableInstance.getAllColsWidth() + this._gantt.getAllDateColsWidth(), totalHeight = this._gantt.getAllRowsHeight();
            clonedContainer.style.width = `${totalWidth}px`, clonedContainer.style.height = `${totalHeight}px`, 
            tempContainer.appendChild(clonedContainer);
            try {
                let GanttClass;
                try {
                    if (GanttClass = (yield import("@visactor/vtable-gantt")).Gantt, !GanttClass) throw new Error("Gantt class not found in @visactor/vtable-gantt");
                } catch (err) {
                    throw new Error("导出甘特图需要安装并正确加载 @visactor/vtable-gantt 依赖\n请执行: npm install @visactor/vtable-gantt");
                }
                const clonedGantt = new GanttClass(clonedContainer, Object.assign(Object.assign({}, this._gantt.options), {
                    records: this._gantt.records,
                    taskListTable: Object.assign(Object.assign({}, this._gantt.options.taskListTable), {
                        tableWidth: void 0,
                        minTableWidth: void 0,
                        maxTableWidth: void 0
                    }),
                    plugins: []
                }));
                return clonedGantt.setPixelRatio(scale), (null === (_a = clonedGantt.scenegraph) || void 0 === _a ? void 0 : _a.ganttGroup) && clonedGantt.scenegraph.ganttGroup.setAttribute("clip", !1), 
                (null === (_c = null === (_b = clonedGantt.taskListTableInstance) || void 0 === _b ? void 0 : _b.scenegraph) || void 0 === _c ? void 0 : _c.tableGroup) && clonedGantt.taskListTableInstance.scenegraph.tableGroup.setAttribute("clip", !1), 
                clonedGantt.scenegraph.stage.render(), {
                    tempContainer: tempContainer,
                    clonedGantt: clonedGantt
                };
            } catch (err) {
                throw tempContainer && tempContainer.parentNode && tempContainer.remove(), new Error("导出甘特图需要安装 @visactor/vtable-gantt 依赖");
            }
        }));
    }
    finalizeExport(canvas, fileName, type, quality, download = !0) {
        const base64 = canvas.toDataURL(`image/${type}`, quality);
        if (download) {
            const link = document.createElement("a");
            link.download = `${fileName}.${type}`, link.href = base64, document.body.appendChild(link), 
            link.click(), document.body.removeChild(link);
        }
        return base64;
    }
    release() {
        this._gantt = null;
    }
}
//# sourceMappingURL=gantt-export-image.js.map