/**
 * YamlEditor - YAML editor with syntax highlighting
 * @countermeasure/web-components/editors
 */

import { type Extension, EditorState } from '@codemirror/state'
import { foldGutter, foldKeymap } from '@codemirror/language'
import { keymap } from '@codemirror/view'
import { BaseEditor } from './base'
import { yaml } from './languages/yaml'
import { type YamlEditorConfig } from './types'

export class YamlEditor extends BaseEditor {
  constructor(config: YamlEditorConfig) {
    const indentSize = config.indentSize ?? 2

    const extraExtensions: Extension[] = [
      // YAML language
      yaml(),

      // Code folding
      foldGutter(),
      keymap.of(foldKeymap),

      // Override tab size for YAML-specific indentation
      EditorState.tabSize.of(indentSize),
    ]

    super(config, extraExtensions)
  }

  /** Get the indent size */
  getIndentSize(): number {
    return this.view.state.tabSize
  }
}
