"use strict";
const keyCodes = require("../../../../base/common/keyCodes.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const range = require("../../../common/core/range.js");
const editorContextKeys = require("../../../common/editorContextKeys.js");
const languageConfigurationRegistry = require("../../../common/languages/languageConfigurationRegistry.js");
const blockCommentCommand = require("./blockCommentCommand.js");
const lineCommentCommand = require("./lineCommentCommand.js");
const nls = require("../../../../nls.js");
const actions = require("../../../../platform/actions/common/actions.js");
class CommentLineAction extends editorExtensions.EditorAction {
  constructor(type, opts) {
    super(opts);
    this._type = type;
  }
  run(accessor, editor) {
    const languageConfigurationService = accessor.get(languageConfigurationRegistry.ILanguageConfigurationService);
    if (!editor.hasModel()) {
      return;
    }
    const model = editor.getModel();
    const commands = [];
    const modelOptions = model.getOptions();
    const commentsOptions = editor.getOption(
      19
      /* comments */
    );
    const selections = editor.getSelections().map((selection, index) => ({ selection, index, ignoreFirstLine: false }));
    selections.sort((a, b) => range.Range.compareRangesUsingStarts(a.selection, b.selection));
    let prev = selections[0];
    for (let i = 1; i < selections.length; i++) {
      const curr = selections[i];
      if (prev.selection.endLineNumber === curr.selection.startLineNumber) {
        if (prev.index < curr.index) {
          curr.ignoreFirstLine = true;
        } else {
          prev.ignoreFirstLine = true;
          prev = curr;
        }
      }
    }
    for (const selection of selections) {
      commands.push(new lineCommentCommand.LineCommentCommand(languageConfigurationService, selection.selection, modelOptions.tabSize, this._type, commentsOptions.insertSpace, commentsOptions.ignoreEmptyLines, selection.ignoreFirstLine));
    }
    editor.pushUndoStop();
    editor.executeCommands(this.id, commands);
    editor.pushUndoStop();
  }
}
class ToggleCommentLineAction extends CommentLineAction {
  constructor() {
    super(0, {
      id: "editor.action.commentLine",
      label: nls.localize("comment.line", "Toggle Line Comment"),
      alias: "Toggle Line Comment",
      precondition: editorContextKeys.EditorContextKeys.writable,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: 2048 | 85,
        weight: 100
        /* EditorContrib */
      },
      menuOpts: {
        menuId: actions.MenuId.MenubarEditMenu,
        group: "5_insert",
        title: nls.localize({ key: "miToggleLineComment", comment: ["&& denotes a mnemonic"] }, "&&Toggle Line Comment"),
        order: 1
      }
    });
  }
}
class AddLineCommentAction extends CommentLineAction {
  constructor() {
    super(1, {
      id: "editor.action.addCommentLine",
      label: nls.localize("comment.line.add", "Add Line Comment"),
      alias: "Add Line Comment",
      precondition: editorContextKeys.EditorContextKeys.writable,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: keyCodes.KeyChord(
          2048 | 41,
          2048 | 33
          /* KeyC */
        ),
        weight: 100
        /* EditorContrib */
      }
    });
  }
}
class RemoveLineCommentAction extends CommentLineAction {
  constructor() {
    super(2, {
      id: "editor.action.removeCommentLine",
      label: nls.localize("comment.line.remove", "Remove Line Comment"),
      alias: "Remove Line Comment",
      precondition: editorContextKeys.EditorContextKeys.writable,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: keyCodes.KeyChord(
          2048 | 41,
          2048 | 51
          /* KeyU */
        ),
        weight: 100
        /* EditorContrib */
      }
    });
  }
}
class BlockCommentAction extends editorExtensions.EditorAction {
  constructor() {
    super({
      id: "editor.action.blockComment",
      label: nls.localize("comment.block", "Toggle Block Comment"),
      alias: "Toggle Block Comment",
      precondition: editorContextKeys.EditorContextKeys.writable,
      kbOpts: {
        kbExpr: editorContextKeys.EditorContextKeys.editorTextFocus,
        primary: 1024 | 512 | 31,
        linux: {
          primary: 2048 | 1024 | 31
          /* KeyA */
        },
        weight: 100
        /* EditorContrib */
      },
      menuOpts: {
        menuId: actions.MenuId.MenubarEditMenu,
        group: "5_insert",
        title: nls.localize({ key: "miToggleBlockComment", comment: ["&& denotes a mnemonic"] }, "Toggle &&Block Comment"),
        order: 2
      }
    });
  }
  run(accessor, editor) {
    const languageConfigurationService = accessor.get(languageConfigurationRegistry.ILanguageConfigurationService);
    if (!editor.hasModel()) {
      return;
    }
    const commentsOptions = editor.getOption(
      19
      /* comments */
    );
    const commands = [];
    const selections = editor.getSelections();
    for (const selection of selections) {
      commands.push(new blockCommentCommand.BlockCommentCommand(selection, commentsOptions.insertSpace, languageConfigurationService));
    }
    editor.pushUndoStop();
    editor.executeCommands(this.id, commands);
    editor.pushUndoStop();
  }
}
editorExtensions.registerEditorAction(ToggleCommentLineAction);
editorExtensions.registerEditorAction(AddLineCommentAction);
editorExtensions.registerEditorAction(RemoveLineCommentAction);
editorExtensions.registerEditorAction(BlockCommentAction);
