const FIELD_MAP = { name: "إسم المنشأة", phone: "رقم التواصل", email: "البريد الالكتروني", city: "المدينة الصناعية", commercialRegister: "السجل التجاري من الاطار", activityCode: "كود النشاط", activity: "النشاط" }; const ICONS = { phone: ``, whatsapp: `` }; const queryInput = document.getElementById("query"); const resultsEl = document.getElementById("results"); const clearButton = document.getElementById("clearButton"); const emptyTemplate = document.getElementById("emptyTemplate"); function escapeHtml(value) { return String(value ?? "") .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); } function normalizeArabic(value) { return String(value || "") .toLowerCase() .replace(/[إأآا]/g, "ا") .replace(/ى/g, "ي") .replace(/ة/g, "ه") .replace(/ؤ/g, "و") .replace(/ئ/g, "ي") .replace(/[ً-ٰٟ]/g, "") .replace(/\s+/g, " ") .trim(); } function digitsOnly(value) { return String(value || "").replace(/\D/g, ""); } function formatPhoneDisplay(value) { const digits = digitsOnly(value); if (!digits || digits === "0") return "غير متاح"; if (digits.length === 9 && digits.startsWith("5")) return `0${digits}`; return digits; } function hasUsefulValue(value) { const text = String(value ?? "").trim(); if (!text) return false; const normalized = text.replace(/\s+/g, "").replace(/[ـ-]/g, ""); if (!normalized) return false; if (/^0+$/.test(digitsOnly(normalized)) && !/[^\d\s+()-]/.test(normalized)) return false; return !["غيرمتاح", "لايوجد", "لاينطبق", "nan", "null", "undefined"].includes(normalizeArabic(normalized)); } function toSaudiInternational(value) { let digits = digitsOnly(value); if (!digits || digits === "0") return ""; if (digits.startsWith("00")) digits = digits.slice(2); if (digits.startsWith("+")) digits = digits.slice(1); if (digits.startsWith("966")) return digits; if (digits.startsWith("0") && digits.length >= 9) return `966${digits.slice(1)}`; if (digits.startsWith("5") && digits.length === 9) return `966${digits}`; return digits.length >= 7 ? digits : ""; } function highlightName(name, query) { const original = String(name || ""); const q = String(query || "").trim(); if (!original || !q) return escapeHtml(original || "-"); const index = original.toLowerCase().indexOf(q.toLowerCase()); if (index === -1) return escapeHtml(original); const before = escapeHtml(original.slice(0, index)); const match = escapeHtml(original.slice(index, index + q.length)); const after = escapeHtml(original.slice(index + q.length)); return `${before}${match}${after}`; } function field(label, value, options = {}) { if (!hasUsefulValue(value)) return ""; const display = value && String(value).trim() ? value : "-"; const content = options.html || escapeHtml(display); const classes = ["info-field", options.full ? "full" : ""].filter(Boolean).join(" "); const valueClasses = ["value", options.ltr ? "ltr" : ""].filter(Boolean).join(" "); const action = options.action || ""; return `