/**version-1.0*/
(function(w) {
	w.commonDataUtil = {
		toHump: function(name) {
			return name.replace(/\_(\w)/g, function(all, letter) {
				return letter.toUpperCase();
			});
		},
		toLine: function(name) {
			return name.replace(/([A-Z])/g, '_$1').toLowerCase();
		},
		toHumpKey: function(data) {
			if (!data) return data;
			var nMap = {};
			for (const key in data) {
				const value = data[key] ?? null;
				const nKey = commonDataUtil.toHump(key);
				nMap[nKey] = value;
			}
			return nMap;
		},
		handleForm: function(data, option) {
			let ignore_fields = ["_X_ROW_CHILD", "_X_ROW_KEY"];
			let ignore_key = "_$ignore_field_";
			let toDo = function(data) {
				if (!Array.isArray(data) && Object.prototype.toString.call(data) !==
					'[object Object]') {
					return data;
				}
				let formData = JSON.parse(JSON.stringify(data || {}));
				let newFormData = {};
				let keys = Object.keys(formData);
				for (let i = 0; i < keys.length; i++) {
					let key = keys[i];
					let value = formData[key] ?? null;
					if (ignore_fields.includes(key) || key.indexOf(ignore_key) == 0) {
						delete formData[key];
						continue;
					}
					if (option && option.ignore_fields && option.ignore_fields.includes(key)) {
						delete formData[key];
						continue;
					}
					if (value != null) {
						if (Array.isArray(value)) {
							value.forEach(function(item, index) {
								if (Array.isArray(item) || Object.prototype.toString.call(
										item) ===
									'[object Object]') {
									value[index] = toDo(item);
								}
							});
							formData[key] = value;
						} else if (Object.prototype.toString.call(value) === '[object Object]') {
							formData[key] = toDo(value);
						} else {
							if (key == 'id' && value.toString().indexOf('row_') == 0) {
								formData[key] = null;
							}
						}
					}
				}
				return formData;
			}
			return toDo(data);
		},
		handleForm2: function(data, field) {
			const formData = {};
			var tKey = field ? field + '.' : '';
			for (const key in data) {
				const rKey = tKey + commonDataUtil.toHump(key);
				let value = data[key] ?? null;
				if (value != null) {
					if (Array.isArray(value)) {
						value.forEach(function(item, index) {
							const mKey = rKey + '[' + index + ']';
							Object.assign(formData, commonDataUtil.handleForm2(item, mKey));
						});
					} else if (Object.prototype.toString.call(value) === '[object Object]') {
						Object.assign(formData, commonDataUtil.handleForm2(value, rKey));
					} else {
						if (key == 'id' && value.toString().indexOf('row_') == 0) {
							value = null;
						} else {
							formData[rKey] = value;
						}
					}
				}
			}
			return formData;
		}
	};

	w.$getVueByUid = function(uid) {
		let loop = function(parent) {
			if (parent._uid == uid) {
				return parent;
			} else {
				let $children = parent.$children;
				for (let i = 0; i < $children.length; i++) {
					let tTarget = loop($children[i]);
					if (tTarget) {
						return tTarget;
					}
				}
			}
		};
		return loop($vueRoot);
	};
})(window);
