import { Disposable } from "../../../base/common/lifecycle.mjs";
import { equalsIgnoreCase, startsWithIgnoreCase } from "../../../base/common/strings.mjs";
import { URI } from "../../../base/common/uri.mjs";
import { createDecorator } from "../../instantiation/common/instantiation.mjs";
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 IOpenerService = createDecorator("openerService");
const NullOpenerService = Object.freeze({
  _serviceBrand: void 0,
  registerOpener() {
    return Disposable.None;
  },
  registerValidator() {
    return Disposable.None;
  },
  registerExternalUriResolver() {
    return Disposable.None;
  },
  setDefaultExternalOpener() {
  },
  registerExternalOpener() {
    return Disposable.None;
  },
  open() {
    return __awaiter(this, void 0, void 0, function* () {
      return false;
    });
  },
  resolveExternalUri(uri) {
    return __awaiter(this, void 0, void 0, function* () {
      return { resolved: uri, dispose() {
      } };
    });
  }
});
function matchesScheme(target, scheme) {
  if (URI.isUri(target)) {
    return equalsIgnoreCase(target.scheme, scheme);
  } else {
    return startsWithIgnoreCase(target, scheme + ":");
  }
}
function matchesSomeScheme(target, ...schemes) {
  return schemes.some((scheme) => matchesScheme(target, scheme));
}
function extractSelection(uri) {
  let selection = void 0;
  const match = /^L?(\d+)(?:,(\d+))?(-L?(\d+)(?:,(\d+))?)?/.exec(uri.fragment);
  if (match) {
    selection = {
      startLineNumber: parseInt(match[1]),
      startColumn: match[2] ? parseInt(match[2]) : 1,
      endLineNumber: match[4] ? parseInt(match[4]) : void 0,
      endColumn: match[4] ? match[5] ? parseInt(match[5]) : 1 : void 0
    };
    uri = uri.with({ fragment: "" });
  }
  return { selection, uri };
}
export {
  IOpenerService,
  NullOpenerService,
  extractSelection,
  matchesScheme,
  matchesSomeScheme
};
