<?php
require_once __DIR__ . '/Support.php';

// Same keys/behavior as the original autoFillValue(h,key) JS function.
function onb_autofill_value(array $h, string $key): string {
    switch ($key) {
        case 'employee_name': return trim(($h['first'] ?? '') . ' ' . ($h['last'] ?? ''));
        case 'first_name_given_name': return $h['first'] ?? '';
        case 'last_name_family_name': return $h['last'] ?? '';
        case 'middle_initial_if_any': return !empty($h['middle']) ? strtoupper($h['middle'][0]) : '';
        case 'phone_number': case 'primary_phone': case 'wotc_phone': return $h['phone'] ?? '';
        case 'email_address': return $h['email'] ?? '';
        case 'address': case 'address_street_number_and_name': case 'wotc_street':
        case 'w4_address': case 'de4_address': return $h['address'] ?? '';
        case 'city': case 'city_or_town': case 'de4_city': return $h['city'] ?? '';
        case 'state': case 'de4_state': return $h['state'] ?? '';
        case 'zip': case 'zip_code': case 'de4_zip': return $h['zip'] ?? '';
        case 'w4_csz': case 'wotc_csz': return onb_city_state_zip($h);
        case 'w4_first': return trim(($h['first'] ?? '') . (!empty($h['middle']) ? ' ' . $h['middle'][0] . '.' : ''));
        case 'w4_last': return $h['last'] ?? '';
        case 'de4_name': case 'wotc_name': return onb_full_name($h);
        case 'bsis_guard_card': return $h['guard_card'] ?? '';
        case 'guard_card_expiry': return !empty($h['guard_card_exp']) ? onb_fmt_mdy($h['guard_card_exp']) : '';
        case 'dl_expiry': return !empty($h['dl_exp']) ? onb_fmt_mdy($h['dl_exp']) : '';
        case 'position': case 'job_title': case 'wotc_position': return $h['position'] ?? '';
        case 'work_location_site': return $h['site'] ?? '';
        case 'pay_rate_amt': return onb_pay_rate_amt($h) !== '' ? ('$' . onb_pay_rate_amt($h)) : '';
        case 'pay_frequency': return $h['pay_freq'] ?? '';
        case 'firearms_permit_status': return $h['firearms_permit'] ?? '';
        case 'employment_status': return $h['emp_status'] ?? '';
        case 'date_of_hire': case 'start_date': case 'wotc_start':
            return !empty($h['hire_date']) ? onb_fmt_mdy($h['hire_date']) : '';
        case 'date':
            return onb_fmt_mdy(date('Y-m-d'));
        case 'employee_id': return $h['eid'] ?? '';
        case 'applicant_name': return onb_full_name($h);
        default: return '';
    }
}
