import { __name } from '../chunk-CGILA3WO.mjs';
import { useState, useEffect } from 'react';
import { FolderOpen, Folder, File } from 'lucide-react';
import { cn } from '@djangocfg/ui-core/lib';
import { jsx } from 'react/jsx-runtime';

// src/tools/FileIcon/loader.ts
var cached;
var inflight = null;
async function loadMaterialIcons() {
  if (cached !== void 0) return cached;
  if (inflight) return inflight;
  inflight = (async () => {
    try {
      const specifier = "material-file-icons";
      const mod = await import(
        /* @vite-ignore */
        specifier
      );
      const fn = mod.getIcon ?? mod.default?.getIcon ?? null;
      cached = fn;
      return fn;
    } catch {
      cached = null;
      return null;
    } finally {
      inflight = null;
    }
  })();
  return inflight;
}
__name(loadMaterialIcons, "loadMaterialIcons");
function getMaterialIconsSync() {
  return cached ?? null;
}
__name(getMaterialIconsSync, "getMaterialIconsSync");
function FileIcon({
  name,
  isFolder = false,
  isExpanded = false,
  size = 16,
  className
}) {
  const [svg, setSvg] = useState(() => {
    if (isFolder) return null;
    const fn = getMaterialIconsSync();
    return fn ? fn(name)?.svg ?? null : null;
  });
  useEffect(() => {
    if (isFolder) {
      setSvg(null);
      return;
    }
    let cancelled = false;
    void loadMaterialIcons().then((fn) => {
      if (cancelled) return;
      setSvg(fn ? fn(name)?.svg ?? null : null);
    });
    return () => {
      cancelled = true;
    };
  }, [isFolder, name]);
  if (isFolder) {
    const Icon = isExpanded ? FolderOpen : Folder;
    return /* @__PURE__ */ jsx(
      Icon,
      {
        className: cn("shrink-0 text-amber-500", className),
        style: { width: size, height: size }
      }
    );
  }
  if (!svg) {
    return /* @__PURE__ */ jsx(
      File,
      {
        className: cn("shrink-0 text-muted-foreground/80", className),
        style: { width: size, height: size }
      }
    );
  }
  return /* @__PURE__ */ jsx(
    "span",
    {
      role: "img",
      "aria-hidden": true,
      className: cn("inline-block shrink-0", className),
      style: { width: size, height: size },
      dangerouslySetInnerHTML: { __html: svg }
    }
  );
}
__name(FileIcon, "FileIcon");
function createFileIconSlot({
  getName,
  size = 16
}) {
  return ({ node, isFolder, isExpanded }) => /* @__PURE__ */ jsx(
    FileIcon,
    {
      name: getName(node),
      isFolder,
      isExpanded,
      size
    }
  );
}
__name(createFileIconSlot, "createFileIconSlot");

export { FileIcon, createFileIconSlot, getMaterialIconsSync, loadMaterialIcons };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map