/** Default company and currency when not specified in URL or on the loaded document. */
export const DEFAULT_COMPANY = 'Seera Arabia';
export const DEFAULT_CURRENCY = 'SAR';

/** Default sales tax template for new Sales Order / Delivery Note / Sales Invoice. */
export const DEFAULT_SALES_TAXES_TEMPLATE = 'KSA Tax Charges - SA';

/** Frappe child tables use `rate`; editors often use `tax_rate`. */
export function taxRatePercent(tx: { tax_rate?: number; rate?: number }): number {
  const v = tx.tax_rate ?? tx.rate;
  return typeof v === 'number' && !Number.isNaN(v) ? v : 0;
}

/** Show SAR when document still has legacy INR (org default is SAR). */
export function displayTxnCurrency(code?: string | null): string {
  const c = (code || '').trim();
  if (!c || c === 'INR') return DEFAULT_CURRENCY;
  return c;
}

/** Display amounts with SAR prefix (org standard; avoids ₹ / INR in UI). */
export function formatOrgCurrencyAmount(value?: number | null): string {
  const n = Number(value ?? 0);
  return `SAR ${n.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
}
