{"version":3,"sources":["../src/services/template/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAqB5C,oCAA6B,SAAQ,OAAO;IAK5B,SAAS,CAAC,aAAa,EAAE,aAAa;IAJlD,OAAO,CAAC,iBAAiB,CAAwC;IACjE,OAAO,CAAC,gBAAgB,CAAoB;IAC5C,OAAO,CAAC,eAAe,CAA4B;gBAE7B,aAAa,EAAE,aAAa;IAQ3C,YAAY,IAAI,MAAM;IAKtB,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,IAAI;IAIpC,IAAI;IAmCV,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,MAAM;IAI7C,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM;IAIvC,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM;IAQrF,OAAO,CAAC,UAAU;CAcrB","file":"index.d.ts","sourcesContent":["import { get as getConfig } from 'config';\nimport * as fs from 'fs';\nimport { compile, HelperDelegate, registerHelper, registerPartial, SafeString, Utils} from 'handlebars';\nimport { Service } from '../../entities/service';\nimport { PluginManager } from '../../plugin.manager';\nimport { Hashtable } from '../../structure';\n\nfunction walkSync(parentDir: string, directory: string, filelist: Array<Array<string>>) {\n    directory = directory || '';\n\n    filelist = filelist || [];\n\n    const files = fs.readdirSync(parentDir + '/' + directory);\n\n    files.forEach((file: string) => {\n        if (fs.statSync(parentDir + '/' + directory + file).isDirectory()) {\n            filelist = walkSync(parentDir, directory + file + '/', filelist);\n        }\n        else {\n            filelist.push([parentDir, directory, file]);\n        }\n    });\n\n    return filelist;\n}\n\nexport class TemplateService extends Service {\n    private compiledTemplates: Hashtable<HandlebarsTemplateDelegate>;\n    private templatesStrings: Hashtable<string>;\n    private helpersRegistry: Hashtable<HelperDelegate>;\n\n    constructor(protected pluginManager: PluginManager) {\n        super(pluginManager);\n\n        this.compiledTemplates = {};\n        this.helpersRegistry = {};\n        this.templatesStrings = {};\n    }\n\n    public getServiceId(): string {\n        return 'core.template';\n    }\n\n    // because helpers could have any signature\n    public addHelper(helperId: string, fn: () => void) {\n        this.helpersRegistry[helperId] = fn;\n    }\n\n    public async init() {\n        for (const helperId of Object.keys(this.helpersRegistry)) {\n            registerHelper(helperId, this.helpersRegistry[helperId]);\n        }\n\n        const partialPaths = getConfig('templates.path.partials') as Array<string>;\n\n        const partialsFiles: Array<Array<string>> = [];\n\n        for (const path of partialPaths) {\n            walkSync(path, '', partialsFiles);\n        }\n\n        for (const file of partialsFiles) {\n            const content = await this.getContent(file[0] + '/' + file[1], file[2]);\n\n            registerPartial(file[2], content);\n        }\n\n        const templatePaths = getConfig('templates.path.templates') as Array<string>;\n\n        const templatesFiles: Array<Array<string>> = [];\n\n        for (const path of templatePaths) {\n            walkSync(path, '', templatesFiles);\n        }\n\n        for (const file of templatesFiles) {\n            const content = await this.getContent(file[0] + '/' +  file[1], file[2]);\n            const compiledTemplate = compile(content);\n            this.compiledTemplates[file[2]] = compiledTemplate;\n            this.templatesStrings[file[2]] = content;\n        }\n    }\n\n    public render(templateId: string, data: any): string {\n        return this.compiledTemplates[templateId](data);\n    }\n\n    public getTemplate(templateId: string): string {\n        return this.templatesStrings[templateId];\n    }\n\n    public renderFromString(template: string, data: Hashtable<any>, lng?: string): string {\n        const compiledTemplate = compile(template);\n        if (lng) {\n            Utils.extend(data, {lng});\n        }\n        return compiledTemplate(data);\n    }\n\n    private getContent(parentDir: string, name: string): Promise<string> {\n        return new Promise((resolve, reject) => {\n            fs.readFile(parentDir  + name, 'utf-8',\n                (error: Error, source: string) => {\n                    if (error) {\n                        reject(error);\n                    }\n                    else {\n                        resolve(source);\n                    }\n                }\n            );\n        });\n    }\n}\n"]}