import {
  CompositeMode,
  ModifyMode,
  TranslateMode,
  type FeatureCollection,
  type ModeProps,
} from '@deck.gl-community/editable-layers'
import { EditType } from '../../../editable/const'

/**
 * Custom composite edit mode that combines translate and modify capabilities for editing drawn geometries.
 *
 * @remarks
 * Extends `CompositeMode` with `TranslateMode` and `ModifyMode`, and adds Enter key support
 * to finalize edits.
 */
export class CustomCompositeEditMode extends CompositeMode {
  constructor() {
    super([new TranslateMode(), new ModifyMode()])
  }

  override handleKeyUp(
    event: KeyboardEvent,
    props: ModeProps<FeatureCollection>,
  ): void {
    super.handleKeyUp(event, props)
    if (event.key === 'Enter') {
      this.resetClickSequence()
      props.onEdit({
        updatedData: props.data,
        editType: EditType.addFeature,
        editContext: {},
      })
    }
  }
}
