import { PHIDInput } from '../../../ui/components/data-entry/phid-input/index.js';
import { withFieldValidation } from '../fragments/with-field-validation/index.js';
const PHIDField = withFieldValidation(PHIDInput, {
    validations: {
        _validPHIDFormat: ({ allowUris, allowedScopes }) => (value) => {
            if (value === '' || value === undefined) {
                return true;
            }
            // URL pattern
            // Domain segments can start/end with alphanumeric and contain hyphens
            // Multiple segments separated by dots are allowed (e.g., sub.domain.com)
            const domainSegment = '[a-zA-Z0-9](?:[a-zA-Z0-9\\-]*[a-zA-Z0-9])?';
            const domain = `${domainSegment}(?:\\.${domainSegment})*`;
            const uuidPattern = '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}';
            const genericIDPattern = '[A-Za-z0-9+/={\}\.\~?&]+';
            const documentIDPattern = `((${genericIDPattern})|(${uuidPattern}))`;
            const URLFormat = `^phd://${domain}/${documentIDPattern}$`;
            // Validate URL format first
            const isValidURLFormat = new RegExp(URLFormat).test(value);
            if (isValidURLFormat) {
                return true;
            }
            // If it's not a URL and URIs are not allowed, return error
            if (!allowUris) {
                return 'Invalid format. Please use URL format: phd://<domain>/<documentID>';
            }
            // URI patterns
            const branchScopePattern = '[a-zA-Z][a-zA-Z0-9\\-/_]*[a-zA-Z0-9]';
            // Valid URI formats
            const URIFormats = [
                `^phd:${documentIDPattern}$`,
                `^phd:${documentIDPattern}:${branchScopePattern}$`,
                `^phd:${documentIDPattern}::${branchScopePattern}$`,
                `^phd:${documentIDPattern}:${branchScopePattern}:${branchScopePattern}$`,
            ];
            const isValidURIFormat = URIFormats.some((format) => new RegExp(format).test(value));
            if (!isValidURIFormat) {
                return 'Invalid format. Please use either URL format: phd://<domain>/<documentID> or URI format: phd:uuid, phd:uuid:branch, phd:uuid::scope, or phd:uuid:branch:scope';
            }
            // Validate scope
            if (Array.isArray(allowedScopes)) {
                const scopeMatch = /.*:.*::([^:]+)$/.exec(value) || /.*:.*:.*:([^:]+)$/.exec(value);
                if (scopeMatch && !allowedScopes.includes(scopeMatch[1])) {
                    return `Invalid scope. Allowed scopes are: ${allowedScopes.join(', ')}`;
                }
            }
            return true;
        },
    },
});
PHIDField.displayName = 'PHIDField';
export { PHIDField };
//# sourceMappingURL=phid-field.js.map