var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { assertExists } from '@blockeditor/global/utils';
import { ZERO_WIDTH_SPACE } from '@blockeditor/inline';
import { ShadowlessElement } from '@blockeditor/lit';
import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { getThemeMode } from '../_common/utils/query.js';
import { DARK_THEME, LIGHT_THEME } from './utils/consts.js';
import { highlightCache, } from './utils/highlight-cache.js';
let AffineCodeLine = class AffineCodeLine extends ShadowlessElement {
    constructor() {
        super(...arguments);
        this.delta = {
            insert: ZERO_WIDTH_SPACE,
        };
        this.highlightOptionsGetter = null;
    }
    render() {
        assertExists(this.highlightOptionsGetter, 'highlightOptionsGetter is not set');
        const { lang, highlighter } = this.highlightOptionsGetter();
        if (!highlighter || !highlighter.getLoadedLanguages().includes(lang)) {
            return html `<span
        ><v-text
          .str=${this.delta.insert}
          .styles=${{
                'word-wrap': 'break-word',
                'white-space': 'break-spaces',
            }}
        ></v-text
      ></span>`;
        }
        const mode = getThemeMode();
        const cacheKey = `${this.delta.insert}-${lang}-${mode}`;
        const cache = highlightCache.get(cacheKey);
        let tokens = [
            {
                content: this.delta.insert,
            },
        ];
        if (cache) {
            tokens = cache;
        }
        else {
            tokens = highlighter.codeToThemedTokens(this.delta.insert, lang, mode === 'dark' ? DARK_THEME : LIGHT_THEME)[0];
            highlightCache.set(cacheKey, tokens);
        }
        const vTexts = tokens.map(token => {
            return html `<v-text
        .str=${token.content}
        .styles=${{
                'word-wrap': 'break-word',
                'white-space': 'break-spaces',
                color: token.color,
            }}
      ></v-text>`;
        });
        return html `<span>${vTexts}</span>`;
    }
};
__decorate([
    property({ type: Object })
], AffineCodeLine.prototype, "delta", void 0);
__decorate([
    property({ attribute: false })
], AffineCodeLine.prototype, "highlightOptionsGetter", void 0);
AffineCodeLine = __decorate([
    customElement('affine-code-line')
], AffineCodeLine);
export { AffineCodeLine };
//# sourceMappingURL=affine-code-line.js.map