import { UrlInput } from '../../../ui/components/data-entry/url-input/index.js';
import { withFieldValidation } from '../fragments/with-field-validation/index.js';
const UrlField = withFieldValidation(UrlInput, {
    validations: {
        _validUrl: () => (value) => {
            if (!value)
                return true;
            try {
                const url = new URL(value);
                const regex = /^(([0-9]{1,3}\.){3}[0-9]{1,3})|(([a-z0-9-_]+\.)+[a-z]{2,})|(localhost)$/i;
                if (!regex.test(url.hostname)) {
                    return `${value} must be a valid URL`;
                }
                return true;
            }
            catch {
                return `${value} must be a valid URL`;
            }
        },
        _allowedProtocols: ({ allowedProtocols }) => (value) => {
            if (!value || !allowedProtocols)
                return true;
            try {
                const url = new URL(value);
                const isAllowed = allowedProtocols.includes(url.protocol.slice(0, -1));
                if (isAllowed)
                    return true;
            }
            catch {
                return true; // handling valid url is handled by _validUrl validation
            }
            let allowedProtocolsString = allowedProtocols.join(', ');
            if (allowedProtocols.length > 1) {
                allowedProtocolsString = allowedProtocolsString.replace(/,\s(?=[^,]*$)/, ' or ');
            }
            return `The URL must start with ${allowedProtocolsString}`;
        },
        _maxURLLength: ({ maxURLLength, label }) => (value) => {
            if (!maxURLLength)
                return true;
            return (value.length <= maxURLLength ||
                `${typeof label === 'string' ? label : 'URL'} must not exceed ${maxURLLength} characters`);
        },
    },
});
UrlField.displayName = 'UrlField';
export { UrlField };
//# sourceMappingURL=url-field.js.map