'use strict';

var DU = require('../../utils/DOMUtils.js').DOMUtils;
var Util = require('../../utils/Util.js').Util;

function saveDataParsoid(options, node) {
	if (DU.isElt(node) && DU.getNodeData(node)) {
		DU.saveDataAttribs(node);
	}
	return true;
}

function dumpDOM(options, root) {
	function cloneData(node, clone) {
		var d = DU.getNodeData(node);
		if (d && d.constructor === Object && d.parsoid && (Object.keys(d.parsoid).length > 0)) {
			DU.setNodeData(clone, Util.clone(d));
			saveDataParsoid(options, clone);
		}

		node = node.firstChild;
		clone = clone.firstChild;
		while (node) {
			cloneData(node, clone);
			node = node.nextSibling;
			clone = clone.nextSibling;
		}
	}

	// cloneNode doesn't clone data => walk DOM to clone it
	var clonedRoot = root.cloneNode(true);
	cloneData(root, clonedRoot);
	console.warn(clonedRoot.outerHTML);
}

if (typeof module === "object") {
	module.exports.dumpDOM = dumpDOM;
}
