/* ============================================================
   file-icons.jsx — logo thuong hieu phan mem cho loai file tai lieu.
   Logo that:
     - Revit, AutoCAD, Autodesk: Simple Icons (CC0)  https://simpleicons.org
     - Word, Excel, PDF: vscode-icons (MIT)  https://github.com/vscode-icons/vscode-icons
   Navisworks (NWC/NWD) dung dau Autodesk (khong co logo rieng cong khai).
   IFC dung huy hieu chu kieu buildingSMART (khong co SVG logo san).
   Map duoi file -> brand. Xuat window.FileTypeIcon({ type, size }).
   ============================================================ */

const FILE_LOGOS = {
  "revit": {
    "vb": "0 0 24 24",
    "fill": "#0696D7",
    "inner": "<path d=\"M24 22.665H2.994c-.519 0-.838-.293-.835-.723.001.042.002-.148.003-.463.142-.083.283-.165.427-.247l.078-.045.07-.058.015-.013.127-.078 1.294-.804h1.134c3.35 0 11.817 0 16.548.007.159 0 .631 0 1.002-.371.371-.372.37-.853.37-1.011-.008-2.057-.001-4.109.005-6.16.008-2.39.016-4.86 0-7.298v-.063l.61-.007c.169-.003.143.197.143.296.014 5.68-.02 11.36.015 17.038zM14.326 8.982c.006-.281.006-.56.006-.859-.009-.5-.395-1.055-.916-1.055-.435 0-.919.006-1.432.006v3.01h1.432c.52 0 .9-.584.91-1.102zM3.887 19.234 3.853 1.363l.007-.025.026-.003h17.313c.51.232.943.56 1.033 1.16.023 1.052 0 1.896 0 2.854.001.023-.002.036 0 .059.03 4.489-.022 8.963-.005 13.453 0 .285-.072.38-.37.38-5.99-.008-17.97-.007-17.97-.007zm5.624-3.971h2.395l.057-.051v-3.5c.316.001.57-.005.787-.005.075 0 .348.075.449.286.36.757.692 1.531 1.125 2.25.583.967 1.704 1.204 2.469 1.204.528 0 .528-.024.528-.245 0-.423-.006-.935-.006-1.374-.403-.039-.734-.163-.929-.541-.362-.705-.74-1.401-1.119-2.114.248-.072.218-.057.302-.092.859-.357 1.139-.951 1.213-1.71.05-.503.059-1.144.025-1.395-.112-.833-.378-1.454-1.036-1.932-.773-.562-1.678-.657-2.582-.687a62.395 62.395 0 0 0-3.678.012v9.894zm-5.658-13.9C1.631 2.64.98 3.087.223 3.513.025 3.622 0 3.895 0 4.1l.02 17.45c.575-.357 3.293-1.96 3.867-2.316L3.853 1.363z\"/>"
  },
  "autocad": {
    "vb": "0 0 24 24",
    "fill": "#E51050",
    "inner": "<path d=\"M3.8672 1.0527v.0157L0 3.3848v17.914l3.8965-2.332h18.3398V2.3301c0-.702-.5773-1.2774-1.2793-1.2774H3.8672zm7.5058 4.0098h3.3008l2.9844 9.9512h-2.5879l-.5683-2.1895h-2.9844l-.5703 2.1621h-2.416l2.8417-9.9238zm11.8633.0273v14.877H4.172l-2.0684 1.2383v.4648c0 .702.5793 1.2774 1.2813 1.2774H24V5.0898h-.7637zM12.9668 6.6816l-.9941 4.3243h2.0468l-1.0527-4.3243z\"/>"
  },
  "autodesk": {
    "vb": "0 0 24 24",
    "fill": "#12A4A6",
    "inner": "<path d=\"m.129 20.202 14.7-9.136h7.625c.235 0 .445.188.445.445 0 .21-.092.305-.21.375l-7.222 4.323c-.47.283-.633.845-.633 1.265l-.008 2.725H24V4.362a.561.561 0 0 0-.585-.562h-8.752L0 12.893V20.2h.129z\"/>"
  },
  "word": {
    "vb": "0 0 32 32",
    "inner": "<defs><linearGradient id=\"word_a\" x1=\"4.494\" y1=\"-1712.086\" x2=\"13.832\" y2=\"-1695.914\" gradientTransform=\"translate(0 1720)\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0\" stop-color=\"#2368c4\"/><stop offset=\"0.5\" stop-color=\"#1a5dbe\"/><stop offset=\"1\" stop-color=\"#1146ac\"/></linearGradient></defs><path d=\"M28.806,3H9.705A1.192,1.192,0,0,0,8.512,4.191h0V9.5l11.069,3.25L30,9.5V4.191A1.192,1.192,0,0,0,28.806,3Z\" style=\"fill:#41a5ee\"/><path d=\"M30,9.5H8.512V16l11.069,1.95L30,16Z\" style=\"fill:#2b7cd3\"/><path d=\"M8.512,16v6.5L18.93,23.8,30,22.5V16Z\" style=\"fill:#185abd\"/><path d=\"M9.705,29h19.1A1.192,1.192,0,0,0,30,27.809h0V22.5H8.512v5.309A1.192,1.192,0,0,0,9.705,29Z\" style=\"fill:#103f91\"/><path d=\"M16.434,8.2H8.512V24.45h7.922a1.2,1.2,0,0,0,1.194-1.191V9.391A1.2,1.2,0,0,0,16.434,8.2Z\" style=\"opacity:0.10000000149011612;isolation:isolate\"/><path d=\"M15.783,8.85H8.512V25.1h7.271a1.2,1.2,0,0,0,1.194-1.191V10.041A1.2,1.2,0,0,0,15.783,8.85Z\" style=\"opacity:0.20000000298023224;isolation:isolate\"/><path d=\"M15.783,8.85H8.512V23.8h7.271a1.2,1.2,0,0,0,1.194-1.191V10.041A1.2,1.2,0,0,0,15.783,8.85Z\" style=\"opacity:0.20000000298023224;isolation:isolate\"/><path d=\"M15.132,8.85H8.512V23.8h6.62a1.2,1.2,0,0,0,1.194-1.191V10.041A1.2,1.2,0,0,0,15.132,8.85Z\" style=\"opacity:0.20000000298023224;isolation:isolate\"/><path d=\"M3.194,8.85H15.132a1.193,1.193,0,0,1,1.194,1.191V21.959a1.193,1.193,0,0,1-1.194,1.191H3.194A1.192,1.192,0,0,1,2,21.959V10.041A1.192,1.192,0,0,1,3.194,8.85Z\" style=\"fill:url(#word_a)\"/><path d=\"M6.9,17.988c.023.184.039.344.046.481h.028c.01-.13.032-.287.065-.47s.062-.338.089-.465l1.255-5.407h1.624l1.3,5.326a7.761,7.761,0,0,1,.162,1h.022a7.6,7.6,0,0,1,.135-.975l1.039-5.358h1.477l-1.824,7.748H10.591L9.354,14.742q-.054-.222-.122-.578t-.084-.52H9.127q-.021.189-.084.561c-.042.249-.075.432-.1.552L7.78,19.871H6.024L4.19,12.127h1.5l1.131,5.418A4.469,4.469,0,0,1,6.9,17.988Z\" style=\"fill:#fff\"/>"
  },
  "excel": {
    "vb": "0 0 32 32",
    "inner": "<defs><linearGradient id=\"excel_a\" x1=\"4.494\" y1=\"-2092.086\" x2=\"13.832\" y2=\"-2075.914\" gradientTransform=\"translate(0 2100)\" gradientUnits=\"userSpaceOnUse\"><stop offset=\"0\" stop-color=\"#18884f\"/><stop offset=\"0.5\" stop-color=\"#117e43\"/><stop offset=\"1\" stop-color=\"#0b6631\"/></linearGradient></defs><path d=\"M19.581,15.35,8.512,13.4V27.809A1.192,1.192,0,0,0,9.705,29h19.1A1.192,1.192,0,0,0,30,27.809h0V22.5Z\" style=\"fill:#185c37\"/><path d=\"M19.581,3H9.705A1.192,1.192,0,0,0,8.512,4.191h0V9.5L19.581,16l5.861,1.95L30,16V9.5Z\" style=\"fill:#21a366\"/><path d=\"M8.512,9.5H19.581V16H8.512Z\" style=\"fill:#107c41\"/><path d=\"M16.434,8.2H8.512V24.45h7.922a1.2,1.2,0,0,0,1.194-1.191V9.391A1.2,1.2,0,0,0,16.434,8.2Z\" style=\"opacity:0.10000000149011612;isolation:isolate\"/><path d=\"M15.783,8.85H8.512V25.1h7.271a1.2,1.2,0,0,0,1.194-1.191V10.041A1.2,1.2,0,0,0,15.783,8.85Z\" style=\"opacity:0.20000000298023224;isolation:isolate\"/><path d=\"M15.783,8.85H8.512V23.8h7.271a1.2,1.2,0,0,0,1.194-1.191V10.041A1.2,1.2,0,0,0,15.783,8.85Z\" style=\"opacity:0.20000000298023224;isolation:isolate\"/><path d=\"M15.132,8.85H8.512V23.8h6.62a1.2,1.2,0,0,0,1.194-1.191V10.041A1.2,1.2,0,0,0,15.132,8.85Z\" style=\"opacity:0.20000000298023224;isolation:isolate\"/><path d=\"M3.194,8.85H15.132a1.193,1.193,0,0,1,1.194,1.191V21.959a1.193,1.193,0,0,1-1.194,1.191H3.194A1.192,1.192,0,0,1,2,21.959V10.041A1.192,1.192,0,0,1,3.194,8.85Z\" style=\"fill:url(#excel_a)\"/><path d=\"M5.7,19.873l2.511-3.884-2.3-3.862H7.758L9.013,14.6c.116.234.2.408.238.524h.017c.082-.188.169-.369.26-.546l1.342-2.447h1.7l-2.359,3.84,2.419,3.905H10.821l-1.45-2.711A2.355,2.355,0,0,1,9.2,16.8H9.176a1.688,1.688,0,0,1-.168.351L7.515,19.873Z\" style=\"fill:#fff\"/><path d=\"M28.806,3H19.581V9.5H30V4.191A1.192,1.192,0,0,0,28.806,3Z\" style=\"fill:#33c481\"/><path d=\"M19.581,16H30v6.5H19.581Z\" style=\"fill:#107c41\"/>"
  },
  "pdf": {
    "vb": "0 0 32 32",
    "inner": "<path d=\"M24.1,2.072h0l5.564,5.8V29.928H8.879V30H29.735V7.945L24.1,2.072\" style=\"fill:#909090\"/><path d=\"M24.031,2H8.808V29.928H29.664V7.873L24.03,2\" style=\"fill:#f4f4f4\"/><path d=\"M8.655,3.5H2.265v6.827h20.1V3.5H8.655\" style=\"fill:#7a7b7c\"/><path d=\"M22.472,10.211H2.395V3.379H22.472v6.832\" style=\"fill:#dd2025\"/><path d=\"M9.052,4.534h-.03l-.207,0H7.745v4.8H8.773V7.715L9,7.728a2.042,2.042,0,0,0,.647-.117,1.427,1.427,0,0,0,.493-.291,1.224,1.224,0,0,0,.335-.454,2.13,2.13,0,0,0,.105-.908,2.237,2.237,0,0,0-.114-.644,1.173,1.173,0,0,0-.687-.65A2.149,2.149,0,0,0,9.37,4.56a2.232,2.232,0,0,0-.319-.026M8.862,6.828l-.089,0V5.348h.193a.57.57,0,0,1,.459.181.92.92,0,0,1,.183.558c0,.246,0,.469-.222.626a.942.942,0,0,1-.524.114\" style=\"fill:#464648\"/><path d=\"M12.533,4.521c-.111,0-.219.008-.295.011L12,4.538h-.78v4.8h.918a2.677,2.677,0,0,0,1.028-.175,1.71,1.71,0,0,0,.68-.491,1.939,1.939,0,0,0,.373-.749,3.728,3.728,0,0,0,.114-.949,4.416,4.416,0,0,0-.087-1.127,1.777,1.777,0,0,0-.4-.733,1.63,1.63,0,0,0-.535-.4,2.413,2.413,0,0,0-.549-.178,1.282,1.282,0,0,0-.228-.017m-.182,3.937-.1,0V5.392h.013a1.062,1.062,0,0,1,.6.107,1.2,1.2,0,0,1,.324.4,1.3,1.3,0,0,1,.142.526c.009.22,0,.4,0,.549a2.926,2.926,0,0,1-.033.513,1.756,1.756,0,0,1-.169.5,1.13,1.13,0,0,1-.363.36.673.673,0,0,1-.416.106\" style=\"fill:#464648\"/><path d=\"M17.43,4.538H15v4.8h1.028V7.434h1.3V6.542h-1.3V5.43h1.4V4.538\" style=\"fill:#464648\"/><path d=\"M21.781,20.255s3.188-.578,3.188.511S22.994,21.412,21.781,20.255Zm-2.357.083a7.543,7.543,0,0,0-1.473.489l.4-.9c.4-.9.815-2.127.815-2.127a14.216,14.216,0,0,0,1.658,2.252,13.033,13.033,0,0,0-1.4.288Zm-1.262-6.5c0-.949.307-1.208.546-1.208s.508.115.517.939a10.787,10.787,0,0,1-.517,2.434A4.426,4.426,0,0,1,18.161,13.841ZM13.513,24.354c-.978-.585,2.051-2.386,2.6-2.444C16.11,21.911,14.537,24.966,13.513,24.354ZM25.9,20.895c-.01-.1-.1-1.207-2.07-1.16a14.228,14.228,0,0,0-2.453.173,12.542,12.542,0,0,1-2.012-2.655,11.76,11.76,0,0,0,.623-3.1c-.029-1.2-.316-1.888-1.236-1.878s-1.054.815-.933,2.013a9.309,9.309,0,0,0,.665,2.338s-.425,1.323-.987,2.639-.946,2.006-.946,2.006a9.622,9.622,0,0,0-2.725,1.4c-.824.767-1.159,1.356-.725,1.945.374.508,1.683.623,2.853-.91a22.549,22.549,0,0,0,1.7-2.492s1.784-.489,2.339-.623,1.226-.24,1.226-.24,1.629,1.639,3.2,1.581,1.495-.939,1.485-1.035\" style=\"fill:#dd2025\"/><path d=\"M23.954,2.077V7.95h5.633L23.954,2.077Z\" style=\"fill:#909090\"/><path d=\"M24.031,2V7.873h5.633L24.031,2Z\" style=\"fill:#f4f4f4\"/><path d=\"M8.975,4.457h-.03l-.207,0H7.668v4.8H8.7V7.639l.228.013a2.042,2.042,0,0,0,.647-.117,1.428,1.428,0,0,0,.493-.291A1.224,1.224,0,0,0,10.4,6.79a2.13,2.13,0,0,0,.105-.908,2.237,2.237,0,0,0-.114-.644,1.173,1.173,0,0,0-.687-.65,2.149,2.149,0,0,0-.411-.105,2.232,2.232,0,0,0-.319-.026M8.785,6.751l-.089,0V5.271H8.89a.57.57,0,0,1,.459.181.92.92,0,0,1,.183.558c0,.246,0,.469-.222.626a.942.942,0,0,1-.524.114\" style=\"fill:#fff\"/><path d=\"M12.456,4.444c-.111,0-.219.008-.295.011l-.235.006h-.78v4.8h.918a2.677,2.677,0,0,0,1.028-.175,1.71,1.71,0,0,0,.68-.491,1.939,1.939,0,0,0,.373-.749,3.728,3.728,0,0,0,.114-.949,4.416,4.416,0,0,0-.087-1.127,1.777,1.777,0,0,0-.4-.733,1.63,1.63,0,0,0-.535-.4,2.413,2.413,0,0,0-.549-.178,1.282,1.282,0,0,0-.228-.017m-.182,3.937-.1,0V5.315h.013a1.062,1.062,0,0,1,.6.107,1.2,1.2,0,0,1,.324.4,1.3,1.3,0,0,1,.142.526c.009.22,0,.4,0,.549a2.926,2.926,0,0,1-.033.513,1.756,1.756,0,0,1-.169.5,1.13,1.13,0,0,1-.363.36.673.673,0,0,1-.416.106\" style=\"fill:#fff\"/><path d=\"M17.353,4.461h-2.43v4.8h1.028V7.357h1.3V6.465h-1.3V5.353h1.4V4.461\" style=\"fill:#fff\"/>"
  }
};

const EXT_BRAND = {
  DOC: "word", DOCX: "word",
  XLS: "excel", XLSX: "excel",
  PDF: "pdf",
};
const BRAND_LABEL = { word: "Microsoft Word", excel: "Microsoft Excel", pdf: "PDF" };
const NAVIS_EXT = { NWC: 1, NWD: 1, NWF: 1 };
const REVIT_EXT = { RVT: 1, RFA: 1, RTE: 1 };
const ACAD_EXT  = { DWG: 1, DXF: 1, DWT: 1 };

/* IFC: trang gấp góc + khối 3D (model) teal + dải mang chữ IFC — đồng bộ Navisworks */
function IfcIcon({ ext = "IFC", size = 22 }) {
  const teal = "#0E9C8E", band = "#0B6F66";
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" style={{ flex: "none", display: "block" }} role="img" aria-label="IFC (buildingSMART)">
      <title>IFC (buildingSMART)</title>
      <path d="M6 3 H20 L26 9 V29 H6 Z" fill="#fff" stroke={teal} strokeWidth="1.4" strokeLinejoin="round" />
      <path d="M20 3 L26 9 H20 Z" fill="#cdeee9" />
      <g fill="none" stroke={teal} strokeWidth="1.4" strokeLinejoin="round" strokeLinecap="round">
        <path d="M16 6 L21.5 9 V14.5 L16 17.5 L10.5 14.5 V9 Z" />
        <path d="M10.5 9 L16 12 L21.5 9" />
        <path d="M16 12 V17.5" />
      </g>
      <rect x="6" y="21.4" width="20" height="7.6" fill={band} />
      <text x="16" y="25.6" textAnchor="middle" dominantBaseline="central" fontFamily="Inter, system-ui, sans-serif" fontWeight="800" fontSize="5.6" letterSpacing="0.3" fill="#fff">{ext}</text>
    </svg>
  );
}

/* Navisworks: trang gấp góc + chữ N xanh + dải navy mang đuôi (NWC/NWD/NWF) */
function NavisworksIcon({ ext = "NWC", size = 22 }) {
  const blue = "#2E5FA3", navy = "#1B2C6B";
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" style={{ flex: "none", display: "block" }} role="img" aria-label={"Navisworks " + ext}>
      <title>{"Navisworks (" + ext + ")"}</title>
      <path d="M6 3 H20 L26 9 V29 H6 Z" fill="#fff" stroke={blue} strokeWidth="1.4" strokeLinejoin="round" />
      <path d="M20 3 L26 9 H20 Z" fill="#cfe0f5" />
      <text x="16" y="12.6" textAnchor="middle" dominantBaseline="central" fontFamily="Inter, system-ui, sans-serif" fontWeight="800" fontSize="12.5" fill={blue}>N</text>
      <rect x="6" y="21.4" width="20" height="7.6" fill={navy} />
      <text x="16" y="25.6" textAnchor="middle" dominantBaseline="central" fontFamily="Inter, system-ui, sans-serif" fontWeight="800" fontSize="5.4" letterSpacing="0.2" fill="#fff">{ext}</text>
    </svg>
  );
}

/* Khung document chung (giống Navisworks/IFC) + nhúng logo hãng thật ở trên + dải đuôi */
function DocBrandIcon({ glyphKey, accent, light, band, ext, label, size = 26 }) {
  const g = FILE_LOGOS[glyphKey];
  const glyph = g ? g.inner : "";
  return React.createElement("svg", {
    width: size, height: size, viewBox: "0 0 32 32", style: { flex: "none", display: "block" },
    role: "img", "aria-label": label,
    dangerouslySetInnerHTML: { __html:
      "<title>" + label + "</title>" +
      '<path d="M6 3 H20 L26 9 V29 H6 Z" fill="#fff" stroke="' + accent + '" stroke-width="1.4" stroke-linejoin="round"/>' +
      '<path d="M20 3 L26 9 H20 Z" fill="' + light + '"/>' +
      '<g transform="translate(9.6 4.8) scale(0.55)" fill="' + accent + '">' + glyph + "</g>" +
      '<rect x="6" y="21.4" width="20" height="7.6" fill="' + band + '"/>' +
      '<text x="16" y="25.6" text-anchor="middle" dominant-baseline="central" font-family="Inter, system-ui, sans-serif" font-weight="800" font-size="5.4" letter-spacing="0.2" fill="#fff">' + ext + "</text>"
    },
  });
}

function FileTypeIcon({ type, size = 26 }) {
  const t = String(type || "").toUpperCase();
  if (t === "IFC" || t === "IFCZIP") return <IfcIcon size={size} />;
  if (NAVIS_EXT[t]) return <NavisworksIcon ext={t} size={size} />;
  if (REVIT_EXT[t]) return <DocBrandIcon glyphKey="revit" accent="#0696D7" light="#cfeaf7" band="#0B5C86" ext={t} label="Autodesk Revit" size={size} />;
  if (ACAD_EXT[t]) return <DocBrandIcon glyphKey="autocad" accent="#E51050" light="#fbd6df" band="#9E0B33" ext={t} label="AutoCAD" size={size} />;
  const key = EXT_BRAND[t];
  const b = key && FILE_LOGOS[key];
  if (!b) return <Icon.doc size={size} style={{ color: "var(--ink-4)" }} />;
  return React.createElement("svg", {
    width: size, height: size, viewBox: b.vb,
    style: { flex: "none", display: "block" },
    fill: b.fill || undefined,
    role: "img", "aria-label": BRAND_LABEL[key] || t,
    dangerouslySetInnerHTML: { __html: "<title>" + (BRAND_LABEL[key] || t) + "</title>" + b.inner },
  });
}

window.FILE_LOGOS = FILE_LOGOS;
window.FileTypeIcon = FileTypeIcon;
