import { saveAs } from "file-saver"; import { _4X4Cert } from "./certs/4X4"; import { CertData } from "./certs/CertData"; import { matpelCert } from "./certs/MATPEL"; import { Matpel } from "./certs/utils"; import { manejoDefensivoCert } from "./certs/MANEJO_DEFENSIVO"; import { mecanicaBasicaCert } from "./certs/MECANICA_BASICA"; import { segOpMaqPesCert } from "./certs/SEG_OP_MAQ_PES"; import { supervisorEscolta } from "./certs/SUPERVISOR_ESCOLTA"; import { ipercCert } from "./certs/IPERC"; declare global { interface Window { // eslint-disable-next-line @typescript-eslint/no-explicit-any docx: any, } } async function download(document: Document, filename: string = "filename.docx") { const blob = await window.docx.Packer.toBlob(document); saveAs(blob, filename); } const certGeneratorGenerator = (generatorFn: (options: CertData) => Promise) => (filename: string, options: CertData) => { generatorFn(options) .then((doc) => { download(doc, filename); }); }; type CertGenObj = { [key: string]: (filename: string, options: CertData) => void } export const certGenerator: CertGenObj = Object.freeze({ "Matpel 1": (filename: string, options: CertData) => { const options_f: CertData = { ...options, matpel: Matpel._1, }; matpelCert(options_f) .then((doc) => { download(doc, filename); }); }, "Matpel 2": (filename: string, options: CertData) => { const options_f: CertData = { ...options, matpel: Matpel._2, }; matpelCert(options_f) .then((doc) => { download(doc, filename); }); }, "Matpel 3": (filename: string, options: CertData) => { const options_f: CertData = { ...options, matpel: Matpel._3, }; matpelCert(options_f) .then((doc) => { download(doc, filename); }); }, "Manejo Defensivo": certGeneratorGenerator(manejoDefensivoCert), "Mecanica Basica": certGeneratorGenerator(mecanicaBasicaCert), "4x4": certGeneratorGenerator(_4X4Cert), "Seg. Op. Maquinaria Pesada": certGeneratorGenerator(segOpMaqPesCert), "Sup. Escolta": certGeneratorGenerator(supervisorEscolta), "IPERC": certGeneratorGenerator(ipercCert), });