import * as tslib_1 from "tslib";
import { Component, Input, ElementRef, ViewChild, ViewEncapsulation, ChangeDetectionStrategy, Renderer2 } from '@angular/core';
import * as prismLib from 'prismjs';
import { OnChange } from '../core/decorators';
var prism = prismLib.default ? prismLib.default : prismLib;
/**
 * Code Highlighter Component
 */
var CodeHighlighterComponent = /** @class */ (function () {
    function CodeHighlighterComponent(el, render) {
        this.el = el;
        this.render = render;
        /**
         * code language, see http://prismjs.com/index.html#languages-list
         * @default typescript
         */
        this.language = 'typescript';
        /**
         * whether show line numbers, default to false
         */
        this.linenumbers = false;
        /**
         * code style theme, todo
         * @docs-private
         */
        this.theme = '';
        /**
         * raw code to be highlighted
         * @default ''
         */
        this.rawCode = '';
    }
    CodeHighlighterComponent.prototype.ngAfterViewInit = function () {
        this.highlight();
    };
    /**
     * @docs-private
     */
    CodeHighlighterComponent.prototype.highlight = function () {
        try {
            if (this.language) {
                // add hooks, see https://github.com/PrismJS/prism/issues/832
                prism.hooks.add('before-highlight', function (env) {
                    env.code = env.element.innerText;
                });
                var codeEl = this._code.nativeElement;
                var contentEl = this._content.nativeElement;
                // get custom `<ng-content>` content
                var contentText = contentEl.innerText.trim();
                // highlight code element
                var htmlCode = prism.highlight(this.rawCode || contentText, prism.languages[this.language]);
                // if no rawCode, highlight custom content text
                this.render.setProperty(codeEl, 'innerHTML', htmlCode);
                // prism.highlightElement(codeEl, true, () => {
                //     console.log('highlight-done');
                // });
                // remove extra ng-content child from dom
                // this.render.removeChild(contentEl.parentNode, contentEl);
            }
        }
        catch (e) {
            throw new Error('cannot highlight code, see error: ' + e);
        }
    };
    CodeHighlighterComponent.prototype.ngOnChanges = function (changes) {
        if ('rawCode' in changes) {
            this.highlight();
        }
    };
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], CodeHighlighterComponent.prototype, "language", void 0);
    tslib_1.__decorate([
        OnChange(true),
        Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], CodeHighlighterComponent.prototype, "linenumbers", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], CodeHighlighterComponent.prototype, "theme", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", Number)
    ], CodeHighlighterComponent.prototype, "linehighlight", void 0);
    tslib_1.__decorate([
        Input(),
        tslib_1.__metadata("design:type", String)
    ], CodeHighlighterComponent.prototype, "rawCode", void 0);
    tslib_1.__decorate([
        ViewChild('code', { static: true }),
        tslib_1.__metadata("design:type", ElementRef)
    ], CodeHighlighterComponent.prototype, "_code", void 0);
    tslib_1.__decorate([
        ViewChild('content', { static: true }),
        tslib_1.__metadata("design:type", ElementRef)
    ], CodeHighlighterComponent.prototype, "_content", void 0);
    CodeHighlighterComponent = tslib_1.__decorate([
        Component({
            selector: 'nb-code-highlighter',
            template: "<pre [class.line-numbers]=\"linenumbers\"><code ngNonBindable #code [ngClass]=\"'language-' + language\"></code></pre><div #content class=\"hide\"><ng-content></ng-content></div>",
            encapsulation: ViewEncapsulation.None,
            changeDetection: ChangeDetectionStrategy.OnPush,
            preserveWhitespaces: true,
            host: {
                'class': 'nb-widget nb-code-highlighter'
            },
            exportAs: 'nbCodeHighlighter'
        }),
        tslib_1.__metadata("design:paramtypes", [ElementRef, Renderer2])
    ], CodeHighlighterComponent);
    return CodeHighlighterComponent;
}());
export { CodeHighlighterComponent };
//# sourceMappingURL=code-highlighter.js.map