/* FreeSize — recursive tree rows */
function TreeRows({ node, depth, parentSize, globalMax, expanded, onToggle, selected, onSelect, onReveal, showMeta }) {
  const { fmt, heatColor, heatT, fmtInt } = window.FZ;
  const isDir = node.type === "dir";
  const isOpen = expanded.has(node.id);
  const share = parentSize > 0 ? node.size / parentSize : 0;
  const t = heatT(node.size, globalMax);
  const color = heatColor(t);
  const sz = window.FZ.formatBytes(node.size);

  const indent = 8 + depth * 18;

  return (
    <React.Fragment>
      <div
        className={"row" + (isDir ? " is-dir" : "") + (selected === node.id ? " sel" : "")}
        onClick={() => onSelect(node.id)}
      >
        <div className="row-name" style={{ paddingLeft: indent }}>
          <button
            className={"caret" + (isOpen ? " open" : "") + (isDir && node.children && node.children.length ? "" : " leaf")}
            onClick={(e) => { e.stopPropagation(); if (isDir) onToggle(node.id); }}
            tabIndex={-1}
          >
            <IconChevron size={15} stroke={2.2} />
          </button>
          <span className={"row-ico" + (isDir ? " dir" : "")}>
            {isDir ? (isOpen ? <IconFolderOpen size={16} /> : <IconFolder size={16} />) : <IconFile size={15} />}
          </span>
          <span className="row-label" title={node.path}>{node.name}</span>
          {showMeta && isDir && (
            <span className="row-meta">{fmtInt(node.files)}&nbsp;Dateien</span>
          )}
        </div>
        <div className="bar-cell">
          <div className={"bar" + (isDir ? "" : " file")}>
            <i style={{ width: Math.max(2, share * 100) + "%", background: color }} />
          </div>
        </div>
        <div className="size-cell">{sz.value}<span className="u">{sz.unit}</span></div>
        <button className="reveal" title="Im Datei-Explorer anzeigen" onClick={(e) => { e.stopPropagation(); onReveal && onReveal(node); }}>
          <IconExternal size={15} />
        </button>
      </div>
      {isDir && isOpen && node.children && node.children.map((c) => (
        <TreeRows
          key={c.id}
          node={c}
          depth={depth + 1}
          parentSize={node.size}
          globalMax={globalMax}
          expanded={expanded}
          onToggle={onToggle}
          selected={selected}
          onSelect={onSelect}
          onReveal={onReveal}
          showMeta={showMeta}
        />
      ))}
    </React.Fragment>
  );
}
window.TreeRows = TreeRows;
