import { writable, derived } from 'svelte/store';
import { browser } from '$app/environment';
import en from './en.json';
import tg from './tg.json';

const DICTS = { en, tg };
const STORAGE_KEY = 'avtoambor.locale';
const DEFAULT_LOCALE = 'tg';

// Warn at most once per missing key, so the console doesn't flood.
const _warned = new Set();

function lookup(dict, key) {
  const parts = key.split('.');
  let v = dict;
  for (const p of parts) {
    if (v == null) return undefined;
    v = v[p];
  }
  return v;
}

export const locale = writable(
  (browser && localStorage.getItem(STORAGE_KEY)) || DEFAULT_LOCALE
);

if (browser) {
  locale.subscribe((value) => {
    try { localStorage.setItem(STORAGE_KEY, value); } catch { /* ignore */ }
    document.documentElement.setAttribute('lang', value);
  });
}

export const t = derived(locale, ($locale) => {
  return (key) => {
    const primary = lookup(DICTS[$locale], key);
    if (primary != null) return primary;
    const fallback = lookup(DICTS.en, key);
    if (fallback != null) {
      if (!_warned.has(key)) {
        _warned.add(key);
        console.warn(`[i18n] missing "${key}" for locale "${$locale}"; using English.`);
      }
      return fallback;
    }
    if (!_warned.has(key)) {
      _warned.add(key);
      console.warn(`[i18n] missing key "${key}"`);
    }
    return key;
  };
});

export function toggleLocale() {
  locale.update((v) => (v === 'en' ? 'tg' : 'en'));
}

// Pick the right column from a record that has both _en and _tg fields,
// e.g. localized(part, 'name', $locale) → part.name_tg or part.name_en.
// Falls back to whichever language has content (not just English) so a
// TG-only entry still renders for an EN viewer.
export function localized(record, baseField, lang) {
  if (!record) return '';
  return (
    record[`${baseField}_${lang}`] ||
    record[`${baseField}_en`] ||
    record[`${baseField}_tg`] ||
    ''
  );
}

// True if the record has a non-empty value in the requested language.
// Used to flag "(missing translation)" when we had to fall back.
export function hasTranslation(record, baseField, lang) {
  if (!record) return false;
  const v = record[`${baseField}_${lang}`];
  return v != null && String(v).trim() !== '';
}

// Money helpers: dirams ↔ display string.
export function formatMoney(dirams, lang = 'en') {
  if (dirams == null) return '';
  const n = Number(dirams) / 100;
  // Tajik uses comma decimal separator in everyday use; English uses period.
  const s = n.toFixed(2);
  return lang === 'tg' ? s.replace('.', ',') : s;
}

export function formatTs(utcStr) {
  if (!utcStr) return '';

  const normalized = String(utcStr).trim().replace(' ', 'T');
  const utcDate = new Date(`${normalized}Z`);
  if (Number.isNaN(utcDate.getTime())) return utcStr;

  const tajikDate = new Date(utcDate.getTime() + 5 * 60 * 60 * 1000);
  const pad = (n) => String(n).padStart(2, '0');

  return [
    pad(tajikDate.getUTCDate()),
    pad(tajikDate.getUTCMonth() + 1),
    tajikDate.getUTCFullYear()
  ].join('.') + ` ${pad(tajikDate.getUTCHours())}:${pad(tajikDate.getUTCMinutes())}`;
}
