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 { ToolbarButtonSlimmer } from './toolbar-button-slimmer.abstract';
import { VegaInternalIconManager } from '../../../../../helpers/icon/internal-icon-manager';
import displayCode from '../../../assets/display-code';
import { MapToComponentField } from 'vega-slimmer/core';
import { VegaRTEContent } from '../../../dto/content-state';
export class SourceEditToolbarButtonSlimmer extends ToolbarButtonSlimmer {
  constructor() {
    super({
      icon: 'display-code',
      tooltip: {
        text: 'Edit Source',
      },
      autoFocusOnClick: false,
    });
  }
  async onClick() {
    const isSelected = this.sourceView;
    if (isSelected) {
      await this.saveSourceCode();
    }
    else {
      this.selectionController.setSelectionRange(new Range());
    }
    this.sourceView = !isSelected;
  }
  isSelected() {
    return this.sourceView;
  }
  isDisabled() {
    return super.isDisabled();
  }
  async saveSourceCode() {
    const sourceViewValue = this.richTextEditorSourceViewRenderer.getSourceCode();
    if (!sourceViewValue && this.value.isEmpty()) {
      return;
    }
    if (typeof sourceViewValue === 'string') {
      await this.valueController.flushChanges(VegaRTEContent.fromHtml(sourceViewValue, {
        autoMatchFormat: this.sourceEditConfig.autoMatchFormat,
        filterInlineStyles: this.sourceEditConfig.filterInlineStyles,
      }));
    }
  }
}
(() => {
  VegaInternalIconManager.register({ 'display-code': displayCode });
})();
__decorate([
  MapToComponentField({ writable: true })
], SourceEditToolbarButtonSlimmer.prototype, "sourceView", void 0);
__decorate([
  MapToComponentField()
], SourceEditToolbarButtonSlimmer.prototype, "selectionController", void 0);
__decorate([
  MapToComponentField()
], SourceEditToolbarButtonSlimmer.prototype, "valueController", void 0);
__decorate([
  MapToComponentField()
], SourceEditToolbarButtonSlimmer.prototype, "richTextEditorSourceViewRenderer", void 0);
__decorate([
  MapToComponentField()
], SourceEditToolbarButtonSlimmer.prototype, "sourceEditConfig", void 0);
__decorate([
  MapToComponentField()
], SourceEditToolbarButtonSlimmer.prototype, "value", void 0);
