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 { Component, Element, Host, h, Prop, Event, Method, Watch } from '@stencil/core';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer, methodPlaceholder, } from 'vega-slimmer/core';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../helpers/slimmers/component-usage-runtime-metrics';
import { sanitizeVegaComponent } from '../../utils/component';
import { VegaCodeBlockRenderer } from './slimmers/renderers/vega-code-block-renderer';
import { arrayToClassString } from '../../utils/ui';
import { VegaCodeBlockContentRenderer } from './slimmers/renderers/vega-code-block-content-renderer';
import { createEventEmitSlimmer, } from '../../helpers/event-manager/slimmers/event-emit-slimmer';
import { VegaChange, VegaRemove } from '../../helpers/event-manager/event-id/vega-event-id';
import { VegaCodeBlockValueController } from './slimmers/controllers/vega-code-block-value-controller';
import { deepEqual } from '../../utils/object';
import { TranslationSlimmer } from '../../helpers/slimmers/translation';
import { CodeContentParseController } from './slimmers/controllers/vega-code-block-content-parse-controller';
import { VegaCodeBlockSelectionController } from './slimmers/controllers/vega-code-block-selection-controller';
import { CodeBlockInputController } from './slimmers/controllers/vega-code-block-input-controller';
import { ChildNodesEventPreventSlimmer } from '../../helpers/event-manager/slimmers/child-nodes-event-prevent-slimmer';
import { VegaCodeBlockFocusController } from './slimmers/controllers/vega-code-block-focus-controller';
import { VegaCodeBlockCopyButtonRenderer } from './slimmers/renderers/vega-code-block-copy-button-renderer';
import { PrismCodeHighlightParseController } from './slimmers/controllers/prism-code-highlight-parse-controller';
import { DefaultCodeHighlightParseController } from './slimmers/controllers/default-code-highlight-parse-controller';
import { ComponentGlobalStyleSlimmer } from '../../helpers/slimmers/component-global-style-slimmer';
import codeColorVariable from './code-color-variable.css';
import { InsertCompositionTextController } from './slimmers/controllers/insert-composition-text-controller';
import { VegaCodeBlockHistoryController } from './slimmers/controllers/vega-code-block-history-controller';
import { CodeBlockPasteController } from './slimmers/controllers/vega-code-block-input-paste-controller';
/**
 * A component for capturing and displaying syntax-highlighted code blocks with copy-to-clipboard functionality.
 *
 * @vegaVersion 2.50.0
 */
export class VegaCodeBlock {
  constructor() {
    this.globalSlimmers = {};
    //TODO: The global style should be removed after the code color change to the color token.
    this.componentGlobalStyleSlimmer = new ComponentGlobalStyleSlimmer(codeColorVariable);
    this.translationSlimmer = new TranslationSlimmer();
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    this.codeBlockRenderer = new VegaCodeBlockRenderer();
    this.contentRenderer = new VegaCodeBlockContentRenderer();
    this.copyButtonRenderer = new VegaCodeBlockCopyButtonRenderer();
    this.valueController = new VegaCodeBlockValueController();
    this.contentParseController = new CodeContentParseController();
    this.prismCodeHighlightParser = new PrismCodeHighlightParseController();
    this.defaultCodeHighlightParser = new DefaultCodeHighlightParseController();
    this.selectionController = new VegaCodeBlockSelectionController();
    this.inputController = new CodeBlockInputController();
    this.insertCompositionTextController = new InsertCompositionTextController();
    this.focusController = new VegaCodeBlockFocusController();
    this.historyController = new VegaCodeBlockHistoryController();
    this.changeEventEmitter = createEventEmitSlimmer(VegaCodeBlock, VegaChange);
    this.removeEventEmitter = createEventEmitSlimmer(VegaCodeBlock, VegaRemove);
    this.codeBlockPasteController = new CodeBlockPasteController();
    this.eventPreventSlimmer = new ChildNodesEventPreventSlimmer([VegaChange], () => this.codeBlockRenderer.getToolbarContainerRef());
    /**
     * Specifies whether the code block editor is in read-only mode.
     *
     * @vegaVersion 2.50.0
     */
    this.readOnly = false;
    /**
     * Specifies whether the code block editor is disabled.
     *
     * @vegaVersion 2.50.0
     */
    this.disabled = false;
    /**
     * Specifies whether to display the copy button in the code block.
     *
     * @vegaVersion 2.50.0
     */
    this.showCopyButton = true;
    /**
     * Specifies whether to display the code line number in the code block.
     *
     * @vegaVersion 2.50.0
     */
    this.showLineNumber = true;
  }
  watchEditableChange() {
    methodPlaceholder();
  }
  /**
   * Check the props value to avoid unnecessary updates.
   *
   * @param {unknown} newValue - The new props value.
   * @param {unknown} oldValue - The old props value.
   * @param {string} name - The property name.
   * @returns {boolean} Return false to skip the current update flow.
   */
  componentShouldUpdate(newValue, oldValue, name) {
    if (name === 'value' && deepEqual(newValue, oldValue)) {
      return false;
    }
    return true;
  }
  /**
   * Invoke this method to focus the current code block.
   *
   * @vegaVersion 2.50.0
   */
  async doFocus() {
    methodPlaceholder();
  }
  render() {
    const classNames = arrayToClassString([
      this.readOnly ? 'readonly' : '',
      this.disabled ? 'disabled' : '',
      this.showLineNumber ? 'show-line-number' : '',
      this.prismCodeHighlightParser.canParse() ? 'highlight' : '',
    ]);
    return sanitizeVegaComponent(h(Host, { contenteditable: false, class: classNames }, this.codeBlockRenderer.render()), this.host);
  }
  static get is() { return "vega-code-block"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-code-block.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-code-block.css"]
  }; }
  static get properties() { return {
    "readOnly": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "Specifies whether the code block editor is in read-only mode."
      },
      "attribute": "read-only",
      "reflect": false,
      "defaultValue": "false"
    },
    "disabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "Specifies whether the code block editor is disabled."
      },
      "attribute": "disabled",
      "reflect": false,
      "defaultValue": "false"
    },
    "showCopyButton": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "Specifies whether to display the copy button in the code block."
      },
      "attribute": "show-copy-button",
      "reflect": false,
      "defaultValue": "true"
    },
    "showLineNumber": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "Specifies whether to display the code line number in the code block."
      },
      "attribute": "show-line-number",
      "reflect": false,
      "defaultValue": "true"
    },
    "value": {
      "type": "unknown",
      "mutable": true,
      "complexType": {
        "original": "VegaCodeBlockValue",
        "resolved": "{ language?: \"css\" | \"html\" | \"js\" | \"jsx\" | \"plainText\" | \"ts\" | \"tsx\"; code: string; }",
        "references": {
          "VegaCodeBlockValue": {
            "location": "import",
            "path": "./types"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "Specifies the content or value to be displayed in the code block."
      }
    }
  }; }
  static get events() { return [{
      "method": "vegaChange",
      "name": "vegaChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "An event emitter notifying a value change."
      },
      "complexType": {
        "original": "VegaCodeBlockValue",
        "resolved": "{ language?: \"css\" | \"html\" | \"js\" | \"jsx\" | \"plainText\" | \"ts\" | \"tsx\"; code: string; }",
        "references": {
          "VegaCodeBlockValue": {
            "location": "import",
            "path": "./types"
          }
        }
      }
    }, {
      "method": "change",
      "name": "change",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }, {
            "name": "eventSemantics",
            "text": "namespace:native"
          }],
        "text": "An event emitter notifying a value change."
      },
      "complexType": {
        "original": "VegaCodeBlockValue",
        "resolved": "{ language?: \"css\" | \"html\" | \"js\" | \"jsx\" | \"plainText\" | \"ts\" | \"tsx\"; code: string; }",
        "references": {
          "VegaCodeBlockValue": {
            "location": "import",
            "path": "./types"
          }
        }
      }
    }, {
      "method": "vegaRemove",
      "name": "vegaRemove",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }],
        "text": "An event emitter notifying the deletion of the code block. By default, the element is removed from the page, but this behavior can be prevented."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "remove",
      "name": "remove",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }, {
            "name": "eventSemantics",
            "text": "namespace:native"
          }],
        "text": "An event emitter notifying the deletion of the code block. By default, the element is removed from the page, but this behavior can be prevented."
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get methods() { return {
    "doFocus": {
      "complexType": {
        "signature": "() => Promise<void>",
        "parameters": [],
        "references": {
          "Promise": {
            "location": "global"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "Invoke this method to focus the current code block.",
        "tags": [{
            "name": "vegaVersion",
            "text": "2.50.0"
          }]
      }
    }
  }; }
  static get elementRef() { return "host"; }
  static get watchers() { return [{
      "propName": "readOnly",
      "methodName": "watchEditableChange"
    }, {
      "propName": "disabled",
      "methodName": "watchEditableChange"
    }]; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaCodeBlock.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "componentGlobalStyleSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "translationSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "codeBlockRenderer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "contentRenderer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "copyButtonRenderer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "valueController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "contentParseController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "prismCodeHighlightParser", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "defaultCodeHighlightParser", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "selectionController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "inputController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "insertCompositionTextController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "focusController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "historyController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "changeEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "removeEventEmitter", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "codeBlockPasteController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaCodeBlock.prototype, "eventPreventSlimmer", void 0);
