// ============================================================
// Konfigurasi Aplikasi (admin dapat ubah manual di sini)
// ============================================================
const APP_NAME = "App Shop";
const APP_VERSION = "0.0.0.1";

// ============================================================
// UUID Generator
// ============================================================
function generateUUID() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0;
    const v = c === "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

// ============================================================
// Safe localStorage access (handle Safari private mode)
// ============================================================
export function getSafeLocalStorageItem(key) {
  try {
    return localStorage.getItem(key);
  } catch (e) {
    console.log("Storage mungkin diblokir - " + e.message);
    return null;
  }
}

export function setSafeLocalStorageItem(key, value) {
  try {
    localStorage.setItem(key, value);
  } catch (e) {
    console.log("Storage mungkin diblokir - " + e.message);
  }
}

export function removeSafeLocalStorageItem(key) {
  try {
    localStorage.removeItem(key);
  } catch (e) {
    console.log("Gagal hapus localStorage " + key + ": " + e.message);
  }
}

// ============================================================
// Fungsi utama: Ambil info device
// ============================================================
export function getDeviceInfo() {
  // Generate atau ambil UUID
  let device_id = getSafeLocalStorageItem("d_id");
  if (!device_id) {
    device_id = generateUUID();
    setSafeLocalStorageItem("d_id", device_id);
  }

  const UserAgent = navigator.userAgent;
  const platform_extend = detectPlatform(UserAgent);
  const device_name = /mobile/i.test(UserAgent) ? "Mobile" : "Desktop";
  const browserDetail = detectBrowser(UserAgent);

  const FullDevice =
    APP_NAME +
    "\n" +
    APP_VERSION +
    "\n" +
    (navigator.platform + " - " + platform_extend) +
    "\n" +
    device_name +
    "\n" +
    browserDetail.full;

  return {
    device_id: device_id,
    platform: navigator.platform + " - " + platform_extend,
    device_name: device_name,
    AppName: APP_NAME,
    AppVersion: APP_VERSION,
    BrowserDetail: browserDetail.full,
    FullDevice: FullDevice,
  };
}

// ============================================================
// Deteksi platform (Windows / Android / iOS / dll)
// ============================================================
function detectPlatform(ua) {
  const devices = {
    iPad: /iPad/.test(ua),
    iPhone: /iPhone/.test(ua),
    Android: /Android/.test(ua),
    Windows: /Windows/.test(ua),
    Mac: /Macintosh|Mac OS X/.test(ua),
    Linux: /Linux/.test(ua),
  };
  let platform = "";
  for (let key in devices) {
    if (devices[key]) {
      platform += key + " ";
    }
  }
  return platform.trim() || "Unknown";
}

// ============================================================
// Deteksi browser & versi (mirip dengan versi PHP-mu)
// ============================================================
function detectBrowser(ua) {
  let name = "Unknown";
  let version = "?";

  if (/MSIE|Trident/i.test(ua)) {
    name = "Internet Explorer";
    version = getVersion(ua, /(MSIE |rv:)([0-9\.]+)/);
  } else if (/Edge/i.test(ua)) {
    name = "Edge";
    version = getVersion(ua, /Edge\/([0-9\.]+)/);
  } else if (/OPR|Opera/i.test(ua)) {
    name = "Opera";
    version = getVersion(ua, /(Opera|OPR)\/([0-9\.]+)/);
  } else if (/Chrome/i.test(ua) && !/Edge/i.test(ua)) {
    name = "Google Chrome";
    version = getVersion(ua, /Chrome\/([0-9\.]+)/);
  } else if (/Safari/i.test(ua) && !/Chrome/i.test(ua)) {
    name = "Safari";
    version = getVersion(ua, /Version\/([0-9\.]+)/);
  } else if (/Firefox/i.test(ua)) {
    name = "Mozilla Firefox";
    version = getVersion(ua, /Firefox\/([0-9\.]+)/);
  } else if (/Netscape/i.test(ua)) {
    name = "Netscape";
  }

  const full = `${name} ${version} | UA: ${ua}`;
  return { name, version, full };
}

// Ambil versi dari regex
function getVersion(ua, regex) {
  const match = ua.match(regex);
  return match ? match[2] || match[1] : "?";
}
