export const $ = (sel, root = document) => root.querySelector(sel);
export const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
let toastTimer = null;
export function toast(message, kind = "") {
const t = $("#toast");
t.textContent = message;
t.className = "toast" + (kind ? ` ${kind}` : "");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => t.classList.add("hidden"), kind === "error" ? 6000 : 2500);
}
export function formatBytes(n) {
if (!Number.isFinite(n) || n <= 0) return "0 B";
const units = ["B", "KB", "MB", "GB", "TB"];
let i = 0;
while (n >= 1024 && i < units.length - 1) (n /= 1024), i++;
return `${n < 10 && i > 0 ? n.toFixed(1) : Math.round(n)} ${units[i]}`;
}
export function icon(id) {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("viewBox", "0 0 24 24");
const use = document.createElementNS("http://www.w3.org/2000/svg", "use");
use.setAttribute("href", `#${id}`);
svg.appendChild(use);
return svg;
}
export function iconButton(iconId, title, onClick, cls = "icon-btn") {
const b = document.createElement("button");
b.type = "button";
b.className = cls;
b.title = title;
b.setAttribute("aria-label", title);
b.appendChild(icon(iconId));
b.addEventListener("click", (e) => onClick(b, e));
return b;
}
export function flashCopied(btn) {
btn.replaceChildren(icon("i-check"));
setTimeout(() => btn.replaceChildren(icon("i-copy")), 1200);
}
export async function copyText(text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
const ta = document.createElement("textarea");
ta.value = text;
ta.style.position = "fixed";
ta.style.opacity = "0";
document.body.appendChild(ta);
ta.select();
let ok = false;
try {
ok = document.execCommand("copy");
} catch {
ok = false;
}
ta.remove();
return ok;
}
}
export function greeting(name) {
const h = new Date().getHours();
const part = h < 5 ? "Good evening" : h < 12 ? "Good morning" : h < 18 ? "Good afternoon" : "Good evening";
return name ? `${part}, ${name}` : part;
}
export function debounce(fn, ms) {
let t;
return (...args) => {
clearTimeout(t);
t = setTimeout(() => fn(...args), ms);
};
}
export function autosize(textarea) {
textarea.style.height = "auto";
textarea.style.height = `${textarea.scrollHeight}px`;
}