
import { StringUtils } from './string-utils';

export class ObjectUtils {

    private static ReviveDateTime(key: any, value: any): any {
        if (typeof value === 'string') {
            let re = new RegExp(/(\d{4})-(\d{2})-(\d{2})/g);
            if (re.exec(value)) {
                return new Date(value);
            }
        }

        return value;
    }
    public static HasAnyData(obj: any, notIncludes: string[] = []) {
        var hasData = false;
        for (const p in obj) {
            if (false == notIncludes.includes(p) &&
                Object.prototype.hasOwnProperty.call(obj, p)) {
                const element = obj[p];
                if (element) {
                    if (typeof element !== 'object' || this.HasAnyData(element, notIncludes)) {
                        hasData = true;
                        console.log(`${p} has value:${element}`);
                        break;
                    }

                }

            }
        }
        return hasData;
    }
    public static CloneValue(obj: any) {
        return JSON.parse(JSON.stringify(obj), ObjectUtils.ReviveDateTime);
        return Object.assign({}, obj);
    }

    public static CopyValue(source: any, destination: any, notIncludes: string[] = ["id"], overwriting: boolean = true) {

        for (const p in source) {
            if (false == notIncludes.includes(p) &&
                Object.prototype.hasOwnProperty.call(source, p)) {
                const element = source[p];
                if (element && Array.isArray(element)) {
                    if (Array.isArray(destination[p])) {

                        for (let i = 0; i < element.length; i++) {
                            const item = element[i];
                            let destLength = destination[p].length;
                            if (i >= destLength) {
                                destination[p] = destination[p].concat([this.CloneValue(destination[p][destLength - 1])]);
                            }
                            this.CopyValue(item, destination[p][i], notIncludes, overwriting);
                        }
                    }
                }
                else if (element && typeof element == 'object') {

                    try {
                        this.CopyValue(element, destination[p], notIncludes, overwriting);
                    } catch (error) {
                        //console.log(`can\'t copy ${p}`, error);

                    }

                } else if (overwriting || [null, '', undefined].includes(destination[p])) {
                    try {
                        destination[p] = element;
                    } catch (error) {
                        //console.log(`can\'t copy ${p}`, error);

                    }

                }
            }
        }
    }
    public static ClearValue(source: any, notIncludes: string[] = ["id"]) {

        for (const p in source) {
            if (false == notIncludes.includes(p) &&
                Object.prototype.hasOwnProperty.call(source, p)) {
                const element = source[p];
                console.log('typeof element', typeof element);

                if (element) {
                    if (typeof element == 'object') {
                        this.ClearValue(element, notIncludes);
                    } else if (typeof element == 'number') {
                        source[p] = 0;
                    } else {
                        source[p] = null;
                    }
                }
            }
        }
    }
}