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;
};
// Lit
import { LitElement, html, css } from 'lit';
import { property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { styleMap } from 'lit/directives/style-map.js';
import { ref, createRef } from 'lit/directives/ref.js';
// Utilities
import { computePopupStyle } from '../utilities/utilities.js';
// Styles
import { backgroundAnimation, underlineAnimation } from '../styles/animation.js';
import { popupStyle } from '../styles/popup.js';
// Default values
import { DEFAULT_ANNOTATION_CONFIG, DEFAULT_POPUP_STYLE, } from '../constants/defaultValues.js';
// Components
import './knob.js';
export class AnnotativeAnnotation extends LitElement {
    constructor() {
        super(...arguments);
        this.annotationRef = createRef();
        this.popupRef = createRef();
        this._showPopup = false;
        this._popupStyle = {};
        this.key = '';
        this.config = DEFAULT_ANNOTATION_CONFIG;
        this.isBgLight = true;
        this.onChange = () => { };
        this.popupStyle = DEFAULT_POPUP_STYLE;
        this.renderPopup = ({ html, key, config, isBgLight, popupStyle = this.popupStyle, }) => {
            const { type, title, description, isEditable, renderDescription } = config;
            return html `<div>
      <div class="">
        <span>${type}</span>
        <span style="color: ${popupStyle.textColor}">${title ?? key}</span>
      </div>
      <div style="color: ${popupStyle.textColor}">
        ${description || isEditable
                ? html `<hr
              class="popup-line-break"
              style="border-color: ${popupStyle.textColor}"
            />`
                : ''}
        <annotative-code-knob
          .key=${key}
          .config=${config}
          .popupStyle=${popupStyle}
          .onChange=${this.onChange}
        ></annotative-code-knob>
        <div class="popup-description">${renderDescription
                ? renderDescription({
                    html,
                    key,
                    config,
                    isBgLight,
                    popupStyle,
                })
                : description
                    ? html `${description}`
                    : ''}</div>
      </div>
    </div>`;
        };
    }
    async _togglePopup() {
        const annotationElement = this.annotationRef;
        if (!annotationElement.value) {
            return;
        }
        if (this._showPopup) {
            this._showPopup = false;
            annotationElement.value.offsetParent.style.zIndex =
                '100';
            return;
        }
        const { style } = computePopupStyle(annotationElement.value);
        this._showPopup = true;
        this._popupStyle = style;
        annotationElement.value.offsetParent.style.zIndex = '110';
    }
    render() {
        const config = { ...DEFAULT_ANNOTATION_CONFIG, ...this.config };
        const { animation, value, formatValue, renderPopup = this.renderPopup, } = config;
        return html `<div
      ${ref(this.annotationRef)}
      id="annotation"
      class=${classMap({
            ...(animation ? { [animation]: true } : null),
        })}
      @mouseover=${this._togglePopup}
      @mouseout=${this._togglePopup}
    >
      <div>${formatValue ? formatValue(value) : value}</div>
      <div
        id="popup"
        ${ref(this.popupRef)}
        class=${classMap({ 'fade-in': this._showPopup })}
        style=${styleMap(this._popupStyle)}
      >
        <div
          id="popup-inner"
          style=${styleMap({
            'color-scheme': this.isBgLight ? 'dark' : 'light',
            'background-color': this.popupStyle.backgroundColor,
            border: `1px solid ${this.popupStyle.borderColor}`,
        })}
        >
          ${renderPopup({
            html,
            key: this.key,
            config,
            isBgLight: this.isBgLight,
            popupStyle: this.popupStyle,
        })}
        </div>
      </div>
    </div>`;
    }
}
AnnotativeAnnotation.styles = [
    backgroundAnimation,
    underlineAnimation,
    popupStyle,
    css `
      :host {
        position: relative;
        display: inline-block;
        vertical-align: top;
        white-space: initial;
        cursor: pointer;
        z-index: 100;
        overflow: visible;
      }
      #annotation {
        position: relative;
        display: inline-block;
        vertical-align: top;
        padding: 0px 4px;
      }
      div.background {
        border-radius: 4px;
        animation: background-animation 1s infinite alternate-reverse;
      }
      div.underline {
        animation: underline-animation 1s infinite alternate-reverse;
      }
    `,
];
__decorate([
    property({ state: true })
], AnnotativeAnnotation.prototype, "_showPopup", void 0);
__decorate([
    property({ state: true })
], AnnotativeAnnotation.prototype, "_popupStyle", void 0);
__decorate([
    property({ attribute: 'key' })
], AnnotativeAnnotation.prototype, "key", void 0);
__decorate([
    property({ attribute: 'config' })
], AnnotativeAnnotation.prototype, "config", void 0);
__decorate([
    property({ attribute: 'isBgLight' })
], AnnotativeAnnotation.prototype, "isBgLight", void 0);
__decorate([
    property({ attribute: 'onChange' })
], AnnotativeAnnotation.prototype, "onChange", void 0);
__decorate([
    property({ attribute: 'popupStyle' })
], AnnotativeAnnotation.prototype, "popupStyle", void 0);
__decorate([
    property({ attribute: 'renderPopup' })
], AnnotativeAnnotation.prototype, "renderPopup", void 0);
customElements.get('annotative-code-annotation') ||
    customElements.define('annotative-code-annotation', AnnotativeAnnotation);
//# sourceMappingURL=annotation.js.map