// ── ตั้งชื่อโฟลเดอร์/ไฟล์ (ปี พ.ศ. ตามเวลาไทย) ─────────────── import { SITE_LABEL, TIME_ZONE } from "./config.js"; const partsFmt = new Intl.DateTimeFormat("en-GB", { timeZone: TIME_ZONE, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false, }); export function bkkParts(date = new Date()) { const p = {}; for (const { type, value } of partsFmt.formatToParts(date)) p[type] = value; const year = Number(p.year); return { year, thaiYear: year + 543, month: p.month, day: p.day, hour: p.hour === "24" ? "00" : p.hour, minute: p.minute, second: p.second, }; } /** "2569.10.25.21.45" — ปี พ.ศ..เดือน.วัน.ชั่วโมง.นาที */ export function folderStamp(date = new Date()) { const p = bkkParts(date); return `${p.thaiYear}.${p.month}.${p.day}.${p.hour}.${p.minute}`; } /** 0→a, 25→z, 26→aa, 27→ab … (แบบคอลัมน์ Excel) */ export function letterForIndex(index) { let n = Math.max(0, Math.trunc(index)) + 1; let s = ""; while (n > 0) { const r = (n - 1) % 26; s = String.fromCharCode(97 + r) + s; n = Math.floor((n - 1) / 26); } return s; } /** a→0, z→25, aa→26 … (คืน -1 ถ้าไม่ใช่ a-z ล้วน) */ export function indexForLetters(letters) { const s = String(letters || "").trim().toLowerCase(); if (!/^[a-z]+$/.test(s)) return -1; let n = 0; for (const ch of s) n = n * 26 + (ch.charCodeAt(0) - 97 + 1); return n - 1; } /** ตัวอักษรถัดไปจากชื่อโฟลเดอร์ที่มีอยู่: Site a …, Site b … */ export function nextLetter(existingNames = []) { let max = -1; for (const name of existingNames) { const m = /^site\s+([a-z]+)\s+\d/.exec(String(name).toLowerCase()); if (m) max = Math.max(max, indexForLetters(m[1])); } return letterForIndex(max + 1); } /** ประกอบชื่อ: Site */ export function makeFolderName({ site = SITE_LABEL, letter = "a", date = new Date(), datetime } = {}) { return `${site} ${letter} ${datetime || folderStamp(date)}`; } /** ชื่อเริ่มต้นของโฟลเดอร์ใหม่ (ไล่ตัวอักษรอัตโนมัติ) */ export function defaultFolderName(existingNames = [], date = new Date()) { return makeFolderName({ letter: nextLetter(existingNames), date }); } /** ตรวจ/ทำความสะอาดค่า datetime จาก CSV → "2569.10.25.21.45" (คืน null ถ้าใช้ไม่ได้) */ export function normalizeFolderStamp(input, fallback = null) { const raw = String(input ?? "").trim(); if (!raw) return fallback; const nums = raw.match(/\d+/g); if (!nums || nums.length < 3) return null; const [y, mo, d, h = "0", mi = "0"] = nums.map((x) => Number(x)); const year = y > 2400 ? y : y + 543; // รับทั้ง ค.ศ. และ พ.ศ. if (year < 2400 || year > 2700) return null; if (mo < 1 || mo > 12 || d < 1 || d > 31 || h > 23 || mi > 59) return null; const p2 = (n) => String(n).padStart(2, "0"); return `${year}.${p2(mo)}.${p2(d)}.${p2(h)}.${p2(mi)}`; } /** EXIF DateTimeOriginal: "YYYY:MM:DD HH:MM:SS" (เวลาไทย) */ export function exifDateString(date = new Date()) { const p = bkkParts(date); return `${p.year}:${p.month}:${p.day} ${p.hour}:${p.minute}:${p.second}`; } /** ชื่อไฟล์: 20261024-171230 */ export function fileStamp(date = new Date()) { const p = bkkParts(date); return `${p.year}${p.month}${p.day}-${p.hour}${p.minute}${p.second}`; } /** วันที่แบบอ่านง่าย: 24/10/2569 17:12 */ export function humanDateTime(ts) { const p = bkkParts(new Date(ts)); return `${p.day}/${p.month}/${p.thaiYear} ${p.hour}:${p.minute}`; } /** ตัดอักขระต้องห้ามของ Windows/Android ออก */ export function safeFileName(name, fallback = "file") { const cleaned = String(name) .replace(/[\\/:*?"<>|\u0000-\u001f]/g, "-") .replace(/\s+/g, " ") .trim() .replace(/^\.+/, ""); return cleaned || fallback; } /** ให้ได้ชื่อไม่ซ้ำในรายการที่มีอยู่: base, base (2), base (3) */ export function uniqueName(base, existingNames) { const taken = new Set(existingNames); if (!taken.has(base)) return base; let n = 2; while (taken.has(`${base} (${n})`)) n++; return `${base} (${n})`; }