export function taxEstimationCalculator(form: FormTs) {
form.addRow(row => {
row.addTextPanel('header', {
computedValue: () => 'Tax Estimation Calculator',
customStyles: { 'font-size': '1.5rem', 'font-weight': '600', 'color': '#1e293b' }
});
});
form.addSpacer({ height: 20 });
// Filing Status Section
const filingSection = form.addSubform('filing', { title: '📋 Filing Status' });
filingSection.addRow(row => {
row.addDropdown('filingStatus', {
label: 'Filing Status',
options: [
{ id: 'single', name: 'Single' },
{ id: 'married-joint', name: 'Married Filing Jointly' },
{ id: 'married-separate', name: 'Married Filing Separately' },
{ id: 'head-household', name: 'Head of Household' }
],
defaultValue: 'single',
isRequired: true
}, '1fr');
row.addDropdown('state', {
label: 'State',
options: [
{ id: 'none', name: 'No State Income Tax' },
{ id: 'low', name: 'Low Tax State (1-4%)' },
{ id: 'medium', name: 'Medium Tax State (4-6%)' },
{ id: 'high', name: 'High Tax State (6-10%)' },
{ id: 'very-high', name: 'Very High Tax State (10%+)' }
],
defaultValue: 'medium',
tooltip: 'Select approximate state tax bracket'
}, '1fr');
});
// Income Section
const incomeSection = form.addSubform('income', { title: '💰 Income' });
incomeSection.addRow(row => {
row.addMoney('wages', {
label: 'Wages & Salary',
min: 0,
max: 10000000,
defaultValue: 75000,
currency: '$',
isRequired: true,
tooltip: 'W-2 income from employment'
}, '1fr');
row.addMoney('selfEmployment', {
label: 'Self-Employment Income',
min: 0,
max: 10000000,
defaultValue: 0,
currency: '$',
tooltip: '1099 income, business profits'
}, '1fr');
});
incomeSection.addRow(row => {
row.addMoney('investments', {
label: 'Investment Income',
min: 0,
max: 10000000,
defaultValue: 0,
currency: '$',
tooltip: 'Dividends, capital gains, interest'
}, '1fr');
row.addMoney('otherIncome', {
label: 'Other Income',
min: 0,
max: 10000000,
defaultValue: 0,
currency: '$',
tooltip: 'Rental income, pensions, etc.'
}, '1fr');
});
// Deductions Section
const deductionsSection = form.addSubform('deductions', { title: '📉 Deductions' });
deductionsSection.addRow(row => {
row.addRadioButton('deductionType', {
label: 'Deduction Type',
options: [
{ id: 'standard', name: 'Standard Deduction' },
{ id: 'itemized', name: 'Itemized Deductions' }
],
defaultValue: 'standard'
});
});
deductionsSection.addRow(row => {
row.addTextPanel('standardDeductionAmount', {
label: 'Standard Deduction',
computedValue: () => {
const status = filingSection.dropdown('filingStatus')?.value() || 'single';
const amounts = {
'single': 14600,
'married-joint': 29200,
'married-separate': 14600,
'head-household': 21900
};
return `$${amounts[status].toLocaleString()} (2024)`;
},
isVisible: () => deductionsSection.radioButton('deductionType')?.value() === 'standard',
customStyles: { 'font-weight': '500', 'color': '#059669' }
});
});
deductionsSection.addRow(row => {
row.addMoney('itemizedDeductions', {
label: 'Total Itemized Deductions',
min: 0,
max: 1000000,
defaultValue: 20000,
currency: '$',
isVisible: () => deductionsSection.radioButton('deductionType')?.value() === 'itemized',
tooltip: 'Mortgage interest, state taxes (up to $10k), charitable donations, etc.'
});
});
// Pre-tax Contributions
const contributionsSection = form.addSubform('contributions', { title: '🏦 Pre-Tax Contributions' });
contributionsSection.addRow(row => {
row.addMoney('retirement401k', {
label: '401(k) Contributions',
min: 0,
max: 23000,
defaultValue: 0,
currency: '$',
tooltip: 'Maximum $23,000 for 2024 ($30,500 if 50+)'
}, '1fr');
row.addMoney('traditionalIRA', {
label: 'Traditional IRA',
min: 0,
max: 7000,
defaultValue: 0,
currency: '$',
tooltip: 'Maximum $7,000 for 2024 ($8,000 if 50+)'
}, '1fr');
});
contributionsSection.addRow(row => {
row.addMoney('hsaContribution', {
label: 'HSA Contributions',
min: 0,
max: 8300,
defaultValue: 0,
currency: '$',
tooltip: 'Maximum $4,150 individual / $8,300 family for 2024'
}, '1fr');
row.addMoney('otherPretax', {
label: 'Other Pre-Tax',
min: 0,
max: 50000,
defaultValue: 0,
currency: '$',
tooltip: 'FSA, dependent care, etc.'
}, '1fr');
});
// Tax Credits Section
const creditsSection = form.addSubform('credits', { title: '✨ Tax Credits' });
creditsSection.addRow(row => {
row.addInteger('dependents', {
label: 'Number of Dependents',
min: 0,
max: 10,
defaultValue: 0,
tooltip: 'Children under 17 qualify for Child Tax Credit'
}, '1fr');
row.addMoney('otherCredits', {
label: 'Other Tax Credits',
min: 0,
max: 50000,
defaultValue: 0,
currency: '$',
tooltip: 'Education credits, energy credits, etc.'
}, '1fr');
});
form.addSpacer({ height: 20, showLine: true, lineStyle: 'dashed' });
// Results Section
const resultsSection = form.addSubform('results', { title: '📊 Tax Breakdown', isCollapsible: false });
// Helper function for tax calculation
const calculateTax = () => {
const status = filingSection.dropdown('filingStatus')?.value() || 'single';
const state = filingSection.dropdown('state')?.value() || 'medium';
const wages = incomeSection.money('wages')?.value() || 0;
const selfEmployment = incomeSection.money('selfEmployment')?.value() || 0;
const investments = incomeSection.money('investments')?.value() || 0;
const otherIncome = incomeSection.money('otherIncome')?.value() || 0;
const totalIncome = wages + selfEmployment + investments + otherIncome;
// Pre-tax deductions
const retirement401k = contributionsSection.money('retirement401k')?.value() || 0;
const traditionalIRA = contributionsSection.money('traditionalIRA')?.value() || 0;
const hsaContribution = contributionsSection.money('hsaContribution')?.value() || 0;
const otherPretax = contributionsSection.money('otherPretax')?.value() || 0;
const totalPretax = retirement401k + traditionalIRA + hsaContribution + otherPretax;
// Standard or itemized deduction
const deductionType = deductionsSection.radioButton('deductionType')?.value() || 'standard';
const standardDeductions = {
'single': 14600,
'married-joint': 29200,
'married-separate': 14600,
'head-household': 21900
};
let deduction = standardDeductions[status];
if (deductionType === 'itemized') {
const itemized = deductionsSection.money('itemizedDeductions')?.value() || 0;
deduction = Math.max(itemized, standardDeductions[status]);
}
// Taxable income
const taxableIncome = Math.max(0, totalIncome - totalPretax - deduction);
// Federal tax brackets 2024
const brackets = {
'single': [
{ limit: 11600, rate: 0.10 },
{ limit: 47150, rate: 0.12 },
{ limit: 100525, rate: 0.22 },
{ limit: 191950, rate: 0.24 },
{ limit: 243725, rate: 0.32 },
{ limit: 609350, rate: 0.35 },
{ limit: Infinity, rate: 0.37 }
],
'married-joint': [
{ limit: 23200, rate: 0.10 },
{ limit: 94300, rate: 0.12 },
{ limit: 201050, rate: 0.22 },
{ limit: 383900, rate: 0.24 },
{ limit: 487450, rate: 0.32 },
{ limit: 731200, rate: 0.35 },
{ limit: Infinity, rate: 0.37 }
],
'married-separate': [
{ limit: 11600, rate: 0.10 },
{ limit: 47150, rate: 0.12 },
{ limit: 100525, rate: 0.22 },
{ limit: 191950, rate: 0.24 },
{ limit: 243725, rate: 0.32 },
{ limit: 365600, rate: 0.35 },
{ limit: Infinity, rate: 0.37 }
],
'head-household': [
{ limit: 16550, rate: 0.10 },
{ limit: 63100, rate: 0.12 },
{ limit: 100500, rate: 0.22 },
{ limit: 191950, rate: 0.24 },
{ limit: 243700, rate: 0.32 },
{ limit: 609350, rate: 0.35 },
{ limit: Infinity, rate: 0.37 }
]
};
// Calculate federal tax
let federalTax = 0;
let remainingIncome = taxableIncome;
let prevLimit = 0;
const statusBrackets = brackets[status] || brackets['single'];
for (const bracket of statusBrackets) {
const taxableInBracket = Math.min(remainingIncome, bracket.limit - prevLimit);
if (taxableInBracket <= 0) break;
federalTax += taxableInBracket * bracket.rate;
remainingIncome -= taxableInBracket;
prevLimit = bracket.limit;
}
// Self-employment tax (15.3% on 92.35% of SE income)
const selfEmploymentTax = selfEmployment * 0.9235 * 0.153;
// State tax estimate
const stateRates = {
'none': 0,
'low': 0.03,
'medium': 0.05,
'high': 0.08,
'very-high': 0.11
};
const stateTax = taxableIncome * (stateRates[state] || 0.05);
// Credits
const dependents = creditsSection.integer('dependents')?.value() || 0;
const childTaxCredit = Math.min(dependents * 2000, federalTax);
const otherCredits = creditsSection.money('otherCredits')?.value() || 0;
const totalCredits = childTaxCredit + otherCredits;
// Final calculations
const totalFederalTax = Math.max(0, federalTax - totalCredits) + selfEmploymentTax;
const totalTax = totalFederalTax + stateTax;
const effectiveRate = totalIncome > 0 ? (totalTax / totalIncome) * 100 : 0;
const takeHome = totalIncome - totalTax - totalPretax;
return {
totalIncome,
taxableIncome,
federalTax: Math.max(0, federalTax - totalCredits),
selfEmploymentTax,
stateTax,
totalCredits,
totalTax,
effectiveRate,
takeHome
};
};
resultsSection.addRow(row => {
row.addPriceDisplay('totalIncome', {
label: 'Total Income',
computedValue: () => calculateTax().totalIncome,
variant: 'default'
}, '1fr');
row.addPriceDisplay('taxableIncome', {
label: 'Taxable Income',
computedValue: () => calculateTax().taxableIncome,
variant: 'default'
}, '1fr');
});
resultsSection.addRow(row => {
row.addPriceDisplay('federalTax', {
label: 'Federal Income Tax',
computedValue: () => calculateTax().federalTax,
variant: 'default'
}, '1fr');
row.addPriceDisplay('selfEmploymentTax', {
label: 'Self-Employment Tax',
computedValue: () => calculateTax().selfEmploymentTax,
variant: 'default',
isVisible: () => (incomeSection.decimal('selfEmployment')?.value() || 0) > 0
}, '1fr');
});
resultsSection.addRow(row => {
row.addPriceDisplay('stateTax', {
label: 'Estimated State Tax',
computedValue: () => calculateTax().stateTax,
variant: 'default'
}, '1fr');
row.addPriceDisplay('totalCredits', {
label: 'Tax Credits Applied',
computedValue: () => calculateTax().totalCredits,
variant: 'success',
isVisible: () => calculateTax().totalCredits > 0
}, '1fr');
});
// Summary Section
const summarySection = form.addSubform('summary', {
title: '💵 Tax Summary',
isCollapsible: false,
sticky: 'bottom'
});
summarySection.addRow(row => {
row.addPriceDisplay('totalTax', {
label: 'Total Estimated Tax',
computedValue: () => calculateTax().totalTax,
variant: 'large'
}, '1fr');
row.addTextPanel('effectiveRate', {
label: 'Effective Tax Rate',
computedValue: () => `${(Number(calculateTax()?.effectiveRate) || 0).toFixed(1)}%`,
customStyles: { 'font-size': '1.5rem', 'font-weight': '600', 'text-align': 'center', 'color': '#dc2626' }
}, '1fr');
});
summarySection.addRow(row => {
row.addPriceDisplay('takeHome', {
label: 'Estimated Take-Home',
computedValue: () => calculateTax().takeHome,
variant: 'success'
});
});
summarySection.addRow(row => {
row.addTextPanel('disclaimer', {
computedValue: () => 'This is an estimate for planning purposes only. Consult a tax professional for accurate advice.',
customStyles: { 'font-size': '0.8rem', 'color': '#64748b', 'text-align': 'center' }
});
});
form.configureSubmitButton({
label: 'Get Detailed Report'
});
}