"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const cancellation = require("../../../../base/common/cancellation.js");
const errors = require("../../../../base/common/errors.js");
const uri = require("../../../../base/common/uri.js");
const range = require("../../../common/core/range.js");
const model = require("../../../common/services/model.js");
const commands = require("../../../../platform/commands/common/commands.js");
const languageFeatures = require("../../../common/services/languageFeatures.js");
function getColors(registry, model2, token) {
  const colors = [];
  const providers = registry.ordered(model2).reverse();
  const promises = providers.map((provider) => Promise.resolve(provider.provideDocumentColors(model2, token)).then((result) => {
    if (Array.isArray(result)) {
      for (let colorInfo of result) {
        colors.push({ colorInfo, provider });
      }
    }
  }));
  return Promise.all(promises).then(() => colors);
}
function getColorPresentations(model2, colorInfo, provider, token) {
  return Promise.resolve(provider.provideColorPresentations(model2, colorInfo, token));
}
commands.CommandsRegistry.registerCommand("_executeDocumentColorProvider", function(accessor, ...args) {
  const [resource] = args;
  if (!(resource instanceof uri.URI)) {
    throw errors.illegalArgument();
  }
  const { colorProvider: colorProviderRegistry } = accessor.get(languageFeatures.ILanguageFeaturesService);
  const model$1 = accessor.get(model.IModelService).getModel(resource);
  if (!model$1) {
    throw errors.illegalArgument();
  }
  const rawCIs = [];
  const providers = colorProviderRegistry.ordered(model$1).reverse();
  const promises = providers.map((provider) => Promise.resolve(provider.provideDocumentColors(model$1, cancellation.CancellationToken.None)).then((result) => {
    if (Array.isArray(result)) {
      for (let ci of result) {
        rawCIs.push({ range: ci.range, color: [ci.color.red, ci.color.green, ci.color.blue, ci.color.alpha] });
      }
    }
  }));
  return Promise.all(promises).then(() => rawCIs);
});
commands.CommandsRegistry.registerCommand("_executeColorPresentationProvider", function(accessor, ...args) {
  const [color, context] = args;
  const { uri: uri$1, range: range$1 } = context;
  if (!(uri$1 instanceof uri.URI) || !Array.isArray(color) || color.length !== 4 || !range.Range.isIRange(range$1)) {
    throw errors.illegalArgument();
  }
  const [red, green, blue, alpha] = color;
  const { colorProvider: colorProviderRegistry } = accessor.get(languageFeatures.ILanguageFeaturesService);
  const model$1 = accessor.get(model.IModelService).getModel(uri$1);
  if (!model$1) {
    throw errors.illegalArgument();
  }
  const colorInfo = {
    range: range$1,
    color: { red, green, blue, alpha }
  };
  const presentations = [];
  const providers = colorProviderRegistry.ordered(model$1).reverse();
  const promises = providers.map((provider) => Promise.resolve(provider.provideColorPresentations(model$1, colorInfo, cancellation.CancellationToken.None)).then((result) => {
    if (Array.isArray(result)) {
      presentations.push(...result);
    }
  }));
  return Promise.all(promises).then(() => presentations);
});
exports.getColorPresentations = getColorPresentations;
exports.getColors = getColors;
