"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const network = require("../../../base/common/network.js");
const resources = require("../../../base/common/resources.js");
const modesRegistry = require("../languages/modesRegistry.js");
const files = require("../../../platform/files/common/files.js");
const fileIconDirectoryRegex = /(?:\/|^)(?:([^\/]+)\/)?([^\/]+)$/;
function getIconClasses(modelService, languageService, resource, fileKind) {
  const classes = fileKind === files.FileKind.ROOT_FOLDER ? ["rootfolder-icon"] : fileKind === files.FileKind.FOLDER ? ["folder-icon"] : ["file-icon"];
  if (resource) {
    let name;
    if (resource.scheme === network.Schemas.data) {
      const metadata = resources.DataUri.parseMetaData(resource);
      name = metadata.get(resources.DataUri.META_DATA_LABEL);
    } else {
      const match = resource.path.match(fileIconDirectoryRegex);
      if (match) {
        name = cssEscape(match[2].toLowerCase());
        if (match[1]) {
          classes.push(`${cssEscape(match[1].toLowerCase())}-name-dir-icon`);
        }
      } else {
        name = cssEscape(resource.authority.toLowerCase());
      }
    }
    if (fileKind === files.FileKind.FOLDER) {
      classes.push(`${name}-name-folder-icon`);
    } else {
      if (name) {
        classes.push(`${name}-name-file-icon`);
        classes.push(`name-file-icon`);
        if (name.length <= 255) {
          const dotSegments = name.split(".");
          for (let i = 1; i < dotSegments.length; i++) {
            classes.push(`${dotSegments.slice(i).join(".")}-ext-file-icon`);
          }
        }
        classes.push(`ext-file-icon`);
      }
      const detectedLanguageId = detectLanguageId(modelService, languageService, resource);
      if (detectedLanguageId) {
        classes.push(`${cssEscape(detectedLanguageId)}-lang-file-icon`);
      }
    }
  }
  return classes;
}
function detectLanguageId(modelService, languageService, resource) {
  if (!resource) {
    return null;
  }
  let languageId = null;
  if (resource.scheme === network.Schemas.data) {
    const metadata = resources.DataUri.parseMetaData(resource);
    const mime = metadata.get(resources.DataUri.META_DATA_MIME);
    if (mime) {
      languageId = languageService.getLanguageIdByMimeType(mime);
    }
  } else {
    const model = modelService.getModel(resource);
    if (model) {
      languageId = model.getLanguageId();
    }
  }
  if (languageId && languageId !== modesRegistry.PLAINTEXT_LANGUAGE_ID) {
    return languageId;
  }
  return languageService.guessLanguageIdByFilepathOrFirstLine(resource);
}
function cssEscape(str) {
  return str.replace(/[\11\12\14\15\40]/g, "/");
}
exports.cssEscape = cssEscape;
exports.getIconClasses = getIconClasses;
