/**
 * 生成指定长度的随机ID
 */
export const generateId = (length: number = 8): string => {
    const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    let result = "";
    for (let i = 0; i < length; i++) {
        result += chars.charAt(Math.floor(Math.random() * chars.length));
    }
    return result;
};

/**
 * 深拷贝对象
 */
export const deepClone = <T>(obj: T): T => {
    if (obj === null || typeof obj !== "object") {
        return obj;
    }

    if (obj instanceof Array) {
        const copy: any[] = [];
        for (let i = 0; i < obj.length; i++) {
            copy[i] = deepClone(obj[i]);
        }
        return copy as unknown as T;
    }

    if (obj instanceof Object) {
        const copy: Record<string, any> = {};
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                copy[key] = deepClone(obj[key]);
            }
        }
        return copy as T;
    }

    return obj;
};
