import Spinner from '@components/admin/Spinner.js';
import { Form } from '@components/common/form/Form.js';
import { InputField } from '@components/common/form/InputField.js';
import { NumberField } from '@components/common/form/NumberField.js';
import { ToggleField } from '@components/common/form/ToggleField.js';
import { Button } from '@components/common/ui/Button.js';
import { toast } from '@components/common/ui/Sonner.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import React from 'react';
import { useForm } from 'react-hook-form';
import { useQuery } from 'urql';
import { TaxRate } from './Rate.js';
// Region keys for the country typed into the form, for the helper list, and
// the retired-key warnings of the rate being edited (spec § 3.3, D-23).
const RegionsQuery = `
query TaxRateRegions($country: String!) {
regions(country: $country) {
key
name
}
}
`;
const WarningsQuery = `
query TaxRateWarnings {
addressConfigWarnings {
kind
source
sourceId
key
keyName
mergedInto {
key
name
}
}
}
`;
/** Collapsible "key — name" list of a country's regions, so the free-text field can be filled without guessing keys. */
function RegionKeyHints({ country }: { country: string }) {
const valid = /^[A-Z]{2}$/.test(country);
const [{ data, fetching }] = useQuery({
query: RegionsQuery,
variables: { country },
pause: !valid
});
const regions: Array<{ key: string; name: string }> = data?.regions ?? [];
if (!valid || fetching || regions.length === 0) return null;
return (
{_('Show region keys for ${country}', { country })}