import commonPasswords from '../data/commonPasswords';
import Trie from './Trie';



const checkStrength = (pass: string) => {
    const score = scorePassword(pass);
    const scoreLabel =  mapScoreToLabel(score);
    return {
        score,
        scoreLabel
    }
};

export default checkStrength;

// Function to score the password based on different criteria
const scorePassword = (password: string): number => {
    if (password.length <= 6) return 0;
    if (isCommonPassword(password)) return 0;

    let score = 0;
    score += getLengthScore(password);
    score += getSpecialCharScore(password);
    score += getCaseMixScore(password);
    score += getNumberMixScore(password);

    return Math.min(score, 4); // Maximum score is 4
};

// Initialize the Trie with common passwords
const trie = new Trie();
commonPasswords.forEach(password => trie.insert(password));
const isCommonPassword = (password: string): boolean => {
    // return commonPasswords.includes(password);
    return trie.search(password);
};

// Function to get the score based on password length
const getLengthScore = (password: string): number => {
    if (password.length > 20 && !hasRepeatChars(password)) return 3;
    if (password.length > 12 && !hasRepeatChars(password)) return 2;
    if (password.length > 8) return 1;
    return 0;
};
// Function to check if the password contains repeated characters
const hasRepeatChars = (password: string): boolean => {
    const repeatCharRegex = /(\w)(\1+\1+\1+\1+)/g;
    return repeatCharRegex.test(password);
};

// Function to get the score based on the presence of special characters
const getSpecialCharScore = (password: string): number => {
    const specialCharRegex = /[^A-Za-z0-9]/g;
    return specialCharRegex.test(password) ? 1 : 0;
};

// Function to get the score based on the mix of uppercase and lowercase letters
const getCaseMixScore = (password: string): number => {
    const hasUpperCase = /[A-Z]/.test(password);
    const hasLowerCase = /[a-z]/.test(password);
    return hasUpperCase && hasLowerCase ? 1 : 0;
};

// Function to get the score based on the mix of letters and numbers
const getNumberMixScore = (password: string): number => {
    const hasLetter = /[A-Za-z]/.test(password);
    const hasNumber = /[0-9]/.test(password);
    return hasLetter && hasNumber ? 1 : 0;
};

// Function to map the score to a corresponding label
const mapScoreToLabel = (score: number): string => {
    const labels = ['risky', 'guessable', 'weak', 'safe', 'secure'];
    return labels[score] || '';
};

// const nameScore = (score: number): string => {
//     switch (score) {
//         case 0:
//             return 'risky';
//         case 1:
//             return 'guessable';
//         case 2:
//             return 'weak';
//         case 3:
//             return 'safe';
//         case 4:
//             return 'secure';
//         default:
//             return '';
//     }
// };
