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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
/**
 * The component value controller.
 */
export class VegaCodeBlockValueController extends VegaSlimmer {
  /**
   * Update the component value if code content or code language update.
   *
   * @param {string} code - The new code content.
   * @param {string} language - The new code language.
   * @param {CodeBlockHistoryState} codeBlockHistoryState - Update the history or not.
   */
  updateValue(code, language, codeBlockHistoryState) {
    if (!this.value || this.value.code !== code || this.value.language !== language) {
      this.value = {
        language: language,
        code,
      };
      this.changeEventEmitter.emit(this.value);
      if (codeBlockHistoryState && codeBlockHistoryState.recordHistory) {
        this.historyController.registerHistory({
          value: this.value,
          offset: codeBlockHistoryState.offset,
        });
      }
    }
  }
  /**
   * Set a default value if the value is not defined.
   */
  initialValue() {
    if (!this.value) {
      this.value = { code: '', language: 'plainText' };
    }
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaCodeBlockValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaCodeBlockValueController.prototype, "historyController", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaCodeBlockValueController.prototype, "initialValue", null);
