export function normalizeUrl(input: string) {
    if (!/^https?:\/\//i.test(input)) {
        return `http://${input}`;
    }
    return input;
}

export function isValidUrlForShortening(input: string): boolean {
    try {
        // Normalize protocol
        const hasProtocol = /^https?:\/\//i.test(input);
        const inputUrl = hasProtocol ? input : `http://${input}`;

        const parsed = new URL(inputUrl);

        // 1. Protocol must be http or https
        if (hasProtocol && !['http:', 'https:'].includes(parsed.protocol)) {
            return false;
        }

        // 2. Port (if present) must be valid
        const port = parsed.port ? parseInt(parsed.port, 10) : null;
        if (port !== null && (port < 1 || port > 65535)) {
            return false;
        }

        // 3. Hostname must exist and be valid
        const host = parsed.hostname;
        if (!host || host.toLowerCase() === 'localhost') {
            return false;
        }

        // ❌ Reject trailing dot (e.g., example.com.)
        if (inputUrl.includes(`${host}.`)) {
            return false;
        }

        // ❌ Reject double dots (e.g., example..com)
        if (host.includes('..')) {
            return false;
        }

        // ❌ Reject domains with leading or trailing hyphens
        const domainParts = host.split('.');
        if (
            domainParts.length < 2 ||
            domainParts.some(
                part => part.startsWith('-') || part.endsWith('-') || part.length === 0
            )
        ) {
            return false;
        }

        // ✅ Accept valid IPv4 address
        const ipMatch = host.match(/^(\d{1,3}\.){3}\d{1,3}$/);
        if (ipMatch) {
            const octets = host.split('.').map(Number);
            if (octets.length === 4 && octets.every(n => n >= 0 && n <= 255)) {
                return true;
            }
            return false;
        }

        // Extract the authority section safely
        const authoritySection = (() => {
            const parts = input.split('//');
            if (parts.length < 2) return '';
            const afterScheme = parts[1];
            return afterScheme.split('/')[0]; // stops before path/query
        })();

        // Count @ signs in authority only
        const atCount = (authoritySection.match(/@/g) || []).length;
        if (atCount > 1) {
            return false;
        }

        // 5. Length limit
        if (inputUrl.length > 2048) {
            return false;
        }

        return true;
    } catch (err) {
        return false;
    }
}
