import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";

import { mergeClassNames } from "../../../common/core/utils";

export function cn(...inputs: ClassValue[]) {
  return mergeClassNames(twMerge, [clsx(inputs)]);
}

export function generateSecurePassword(length = 16): string {
  const charset =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+~`|}{[]:;?><,./-=";
  let password = "";
  if (typeof crypto !== "undefined" && crypto.getRandomValues) {
    const values = new Uint32Array(length);
    crypto.getRandomValues(values);
    for (let i = 0; i < length; i++) {
      password += charset[values[i] % charset.length];
    }
  } else {
    // Fallback for older environments
    for (let i = 0; i < length; i++) {
      password += charset[Math.floor(Math.random() * charset.length)];
    }
  }
  return password;
}
