/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export const deepClone = (source: any) => {
    if (!source && typeof source !== 'object') {
        throw new Error('error arguments', 'deepClone' as any);
    }
    const targetObj: any = source.constructor === Array ? [] : {};
    Object.keys(source).forEach((keys) => {
        if (source[keys] && typeof source[keys] === 'object') {
            targetObj[keys] = deepClone(source[keys]);
        } else {
            targetObj[keys] = source[keys];
        }
    });
    return targetObj;
};

export const getRgba = (rgbaString) => {
    // 使用正则表达式匹配rgba的各个部分
    const match = rgbaString.match(/^rgba\(\s*(\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\s*\)$/);
    if (!match) {
        return rgbaString;
    }

    // 提取出红、绿、蓝和原始透明度
    const [r, g, b, opacity] = match.slice(1).map(Number);

    // 返回新的RGBA字符串，其中透明度被设置为1
    return {
        color: 'rgb(' + r + ',' + g + ',' + b + ')',
        opacity: opacity
    }
}
