"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const lifecycle = require("../../../../base/common/lifecycle.js");
const resources = require("../../../../base/common/resources.js");
const editorExtensions = require("../../../browser/editorExtensions.js");
const codeEditorService = require("../../../browser/services/codeEditorService.js");
const nls = require("../../../../nls.js");
const dialogs = require("../../../../platform/dialogs/common/dialogs.js");
var __decorate = globalThis && globalThis.__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;
};
var __param = globalThis && globalThis.__param || function(paramIndex, decorator) {
  return function(target, key) {
    decorator(target, key, paramIndex);
  };
};
var __awaiter = globalThis && globalThis.__awaiter || function(thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function(resolve) {
      resolve(value);
    });
  }
  return new (P || (P = Promise))(function(resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }
    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }
    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }
    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};
const ignoreUnusualLineTerminators = "ignoreUnusualLineTerminators";
function writeIgnoreState(codeEditorService2, model, state) {
  codeEditorService2.setModelProperty(model.uri, ignoreUnusualLineTerminators, state);
}
function readIgnoreState(codeEditorService2, model) {
  return codeEditorService2.getModelProperty(model.uri, ignoreUnusualLineTerminators);
}
exports.UnusualLineTerminatorsDetector = class UnusualLineTerminatorsDetector extends lifecycle.Disposable {
  constructor(_editor, _dialogService, _codeEditorService) {
    super();
    this._editor = _editor;
    this._dialogService = _dialogService;
    this._codeEditorService = _codeEditorService;
    this._config = this._editor.getOption(
      114
      /* unusualLineTerminators */
    );
    this._register(this._editor.onDidChangeConfiguration((e) => {
      if (e.hasChanged(
        114
        /* unusualLineTerminators */
      )) {
        this._config = this._editor.getOption(
          114
          /* unusualLineTerminators */
        );
        this._checkForUnusualLineTerminators();
      }
    }));
    this._register(this._editor.onDidChangeModel(() => {
      this._checkForUnusualLineTerminators();
    }));
    this._register(this._editor.onDidChangeModelContent((e) => {
      if (e.isUndoing) {
        return;
      }
      this._checkForUnusualLineTerminators();
    }));
  }
  _checkForUnusualLineTerminators() {
    return __awaiter(this, void 0, void 0, function* () {
      if (this._config === "off") {
        return;
      }
      if (!this._editor.hasModel()) {
        return;
      }
      const model = this._editor.getModel();
      if (!model.mightContainUnusualLineTerminators()) {
        return;
      }
      const ignoreState = readIgnoreState(this._codeEditorService, model);
      if (ignoreState === true) {
        return;
      }
      if (this._editor.getOption(
        81
        /* readOnly */
      )) {
        return;
      }
      if (this._config === "auto") {
        model.removeUnusualLineTerminators(this._editor.getSelections());
        return;
      }
      const result = yield this._dialogService.confirm({
        title: nls.localize("unusualLineTerminators.title", "Unusual Line Terminators"),
        message: nls.localize("unusualLineTerminators.message", "Detected unusual line terminators"),
        detail: nls.localize("unusualLineTerminators.detail", "The file '{0}' contains one or more unusual line terminator characters, like Line Separator (LS) or Paragraph Separator (PS).\n\nIt is recommended to remove them from the file. This can be configured via `editor.unusualLineTerminators`.", resources.basename(model.uri)),
        primaryButton: nls.localize("unusualLineTerminators.fix", "Remove Unusual Line Terminators"),
        secondaryButton: nls.localize("unusualLineTerminators.ignore", "Ignore")
      });
      if (!result.confirmed) {
        writeIgnoreState(this._codeEditorService, model, true);
        return;
      }
      model.removeUnusualLineTerminators(this._editor.getSelections());
    });
  }
};
exports.UnusualLineTerminatorsDetector.ID = "editor.contrib.unusualLineTerminatorsDetector";
exports.UnusualLineTerminatorsDetector = __decorate([
  __param(1, dialogs.IDialogService),
  __param(2, codeEditorService.ICodeEditorService)
], exports.UnusualLineTerminatorsDetector);
editorExtensions.registerEditorContribution(exports.UnusualLineTerminatorsDetector.ID, exports.UnusualLineTerminatorsDetector);
