import { jsx as _jsx } from "react/jsx-runtime";
import { Select } from "#ui";
import React from "react";
import { CircleFlag } from "react-circle-flags";
import countries from "world-countries";
import { withFieldValidation } from "../fragments/with-field-validation/with-field-validation.js";
const CountryCodeFieldRaw = React.forwardRef(({ onChange, placeholder, allowedCountries, excludedCountries, includeDependentAreas = false, viewMode = "NamesOnly", showFlagIcons = true, enableSearch, ...props }, ref) => {
    const defaultOptions = countries
        .filter((country) => (includeDependentAreas ? true : country.independent) &&
        country.cca2 !== "AQ")
        .map((country) => ({
        value: country.cca2,
        label: viewMode === "CodesOnly"
            ? country.cca2
            : viewMode === "NamesAndCodes"
                ? `${country.name.common} (${country.cca2})`
                : country.name.common,
        icon: showFlagIcons
            ? () => (_jsx(CircleFlag, { className: "size-4", countryCode: country.cca2.toLowerCase(), height: 16 }))
            : undefined,
    }))
        .sort((a, b) => (a.label > b.label ? 1 : a.label < b.label ? -1 : 0));
    const options = Array.isArray(allowedCountries) || Array.isArray(excludedCountries)
        ? defaultOptions.filter((option) => (!allowedCountries || allowedCountries.includes(option.value)) &&
            !excludedCountries?.includes(option.value))
        : defaultOptions;
    return (_jsx(Select, { ref: ref, options: options, selectionIcon: "checkmark", selectionIconPosition: "right", searchable: enableSearch, onChange: onChange, placeholder: placeholder, ...props }));
});
export const CountryCodeField = withFieldValidation(CountryCodeFieldRaw, {
    validations: {
        _validOption: ({ allowedCountries, excludedCountries, includeDependentAreas }) => (value) => {
            if (value === "" || value === undefined) {
                return true;
            }
            const validCountries = countries
                .filter((country) => (includeDependentAreas ? true : country.independent) &&
                country.cca2 !== "AQ")
                .map((country) => country.cca2);
            // First check if it's a valid country code
            if (!validCountries.includes(value)) {
                return "Please select a valid country";
            }
            // Check if country is in allowed list
            if (Array.isArray(allowedCountries) &&
                !allowedCountries.includes(value)) {
                return "Please select a valid country";
            }
            // Check if country is in excluded list
            if (Array.isArray(excludedCountries) &&
                excludedCountries.includes(value)) {
                return "Please select a valid country";
            }
            return true;
        },
    },
});
CountryCodeField.displayName = "CountryCodeField";
//# sourceMappingURL=country-code-field.js.map