import { TaxData } from '../../types';
import { ab } from '../ab';
import { randomDelay } from '../anti-detection';

/**
 * Extract tax data from the Tax tab.
 */
export async function extractTax(): Promise<TaxData> {
  // Click Tax tab
  ab('find role tab click --name "Tax"');
  await randomDelay(3000, 5000);

  const snap = ab('snapshot -i');
  return parseTaxFromSnapshot(snap.output || '');
}

export function parseTaxFromSnapshot(snapshot: string): TaxData {
  if (!snapshot) return {};

  const data: TaxData = {};

  // Assessed Value
  const assessedMatch = snapshot.match(/(?:Assessed Value|Total Assessed)[:\s]*\$?([\d,]+)/i);
  if (assessedMatch) data.assessedValue = assessedMatch[1].replace(/,/g, '');

  // Tax Amount
  const taxMatch = snapshot.match(/(?:Tax Amount|Annual Tax|Property Tax)[:\s]*\$?([\d,]+)/i);
  if (taxMatch) data.taxAmount = taxMatch[1].replace(/,/g, '');

  // Tax Year
  const yearMatch = snapshot.match(/(?:Tax Year|Year)[:\s]*(\d{4})/i);
  if (yearMatch) data.taxYear = yearMatch[1];

  return data;
}
