import {appPath, Singleton} from '@extollo/lib'
import {MarkMark} from '../../markmark/types'
import {Parser} from '../../markmark/parser'
import {HtmlRenderer} from '../../markmark/html.renderer'
import {MarkMarkRenderer} from '../../markmark/markmark.renderer'

@Singleton()
export class MarkMarkService {
    private cachedLinks?: MarkMark
    private cachedLinksHTML?: string
    private cachedLinksMM?: string

    public async getLinks(): Promise<MarkMark> {
        if ( this.cachedLinks ) {
            return this.cachedLinks
        }

        const path = appPath('resources', 'markmark', 'links.mark.md')
        const content = await path.read()
        return (this.cachedLinks = (new Parser()).parse(content))
    }

    public async getLinksHTML(): Promise<string> {
        if ( this.cachedLinksHTML ) {
            return this.cachedLinksHTML
        }

        const mm = await this.getLinks()
        return (this.cachedLinksHTML = (new HtmlRenderer()).render(mm))
    }

    public async getLinksMM(): Promise<string> {
        if ( this.cachedLinksMM ) {
            return this.cachedLinksMM
        }

        const mm = await this.getLinks()
        return (this.cachedLinksMM = (new MarkMarkRenderer()).render(mm))
    }
}
