"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const dom = require("../../../base/browser/dom.js");
const platform = require("../../../base/common/platform.js");
const touch = require("../../../base/browser/touch.js");
const lifecycle = require("../../../base/common/lifecycle.js");
const mouseHandler = require("./mouseHandler.js");
const editorDom = require("../editorDom.js");
const canIUse = require("../../../base/browser/canIUse.js");
const textAreaInput = require("./textAreaInput.js");
class PointerEventHandler extends mouseHandler.MouseHandler {
  constructor(context, viewController, viewHelper) {
    super(context, viewController, viewHelper);
    this._register(touch.Gesture.addTarget(this.viewHelper.linesContentDomNode));
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, touch.EventType.Tap, (e) => this.onTap(e)));
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, touch.EventType.Change, (e) => this.onChange(e)));
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, touch.EventType.Contextmenu, (e) => this._onContextMenu(new editorDom.EditorMouseEvent(e, this.viewHelper.viewDomNode), false)));
    this._lastPointerType = "mouse";
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, "pointerdown", (e) => {
      const pointerType = e.pointerType;
      if (pointerType === "mouse") {
        this._lastPointerType = "mouse";
        return;
      } else if (pointerType === "touch") {
        this._lastPointerType = "touch";
      } else {
        this._lastPointerType = "pen";
      }
    }));
    const pointerEvents = new editorDom.EditorPointerEventFactory(this.viewHelper.viewDomNode);
    this._register(pointerEvents.onPointerMoveThrottled(this.viewHelper.viewDomNode, (e) => this._onMouseMove(e), mouseHandler.createMouseMoveEventMerger(this.mouseTargetFactory), mouseHandler.MouseHandler.MOUSE_MOVE_MINIMUM_TIME));
    this._register(pointerEvents.onPointerUp(this.viewHelper.viewDomNode, (e) => this._onMouseUp(e)));
    this._register(pointerEvents.onPointerLeave(this.viewHelper.viewDomNode, (e) => this._onMouseLeave(e)));
    this._register(pointerEvents.onPointerDown(this.viewHelper.viewDomNode, (e) => this._onMouseDown(e)));
  }
  onTap(event) {
    if (!event.initialTarget || !this.viewHelper.linesContentDomNode.contains(event.initialTarget)) {
      return;
    }
    event.preventDefault();
    this.viewHelper.focusTextArea();
    const target = this._createMouseTarget(new editorDom.EditorMouseEvent(event, this.viewHelper.viewDomNode), false);
    if (target.position) {
      this.viewController.dispatchMouse({
        position: target.position,
        mouseColumn: target.position.column,
        startedOnLineNumbers: false,
        mouseDownCount: event.tapCount,
        inSelectionMode: false,
        altKey: false,
        ctrlKey: false,
        metaKey: false,
        shiftKey: false,
        leftButton: false,
        middleButton: false,
        onInjectedText: target.type === 6 && target.detail.injectedText !== null
      });
    }
  }
  onChange(e) {
    if (this._lastPointerType === "touch") {
      this._context.viewModel.viewLayout.deltaScrollNow(-e.translationX, -e.translationY);
    }
  }
  _onMouseDown(e) {
    if (e.browserEvent.pointerType === "touch") {
      return;
    }
    super._onMouseDown(e);
  }
}
class TouchHandler extends mouseHandler.MouseHandler {
  constructor(context, viewController, viewHelper) {
    super(context, viewController, viewHelper);
    this._register(touch.Gesture.addTarget(this.viewHelper.linesContentDomNode));
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, touch.EventType.Tap, (e) => this.onTap(e)));
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, touch.EventType.Change, (e) => this.onChange(e)));
    this._register(dom.addDisposableListener(this.viewHelper.linesContentDomNode, touch.EventType.Contextmenu, (e) => this._onContextMenu(new editorDom.EditorMouseEvent(e, this.viewHelper.viewDomNode), false)));
  }
  onTap(event) {
    event.preventDefault();
    this.viewHelper.focusTextArea();
    const target = this._createMouseTarget(new editorDom.EditorMouseEvent(event, this.viewHelper.viewDomNode), false);
    if (target.position) {
      const event2 = document.createEvent("CustomEvent");
      event2.initEvent(textAreaInput.TextAreaSyntethicEvents.Tap, false, true);
      this.viewHelper.dispatchTextAreaEvent(event2);
      this.viewController.moveTo(target.position);
    }
  }
  onChange(e) {
    this._context.viewModel.viewLayout.deltaScrollNow(-e.translationX, -e.translationY);
  }
}
class PointerHandler extends lifecycle.Disposable {
  constructor(context, viewController, viewHelper) {
    super();
    if (platform.isIOS && canIUse.BrowserFeatures.pointerEvents) {
      this.handler = this._register(new PointerEventHandler(context, viewController, viewHelper));
    } else if (window.TouchEvent) {
      this.handler = this._register(new TouchHandler(context, viewController, viewHelper));
    } else {
      this.handler = this._register(new mouseHandler.MouseHandler(context, viewController, viewHelper));
    }
  }
  getTargetAtClientPoint(clientX, clientY) {
    return this.handler.getTargetAtClientPoint(clientX, clientY);
  }
}
exports.PointerEventHandler = PointerEventHandler;
exports.PointerHandler = PointerHandler;
