import addElOrigin from "./addElOrigin"
import mapInnerHTMLAssignment from "./mapInnerHTMLAssignment"
import Origin from "../origin"

export default function processElementsAvailableOnInitialLoad(){
    var originalHtml = decodeURIComponent(document.getElementById("fromjs-initial-html").innerHTML)

    // replace everythign before body tag
    var htmlWithoutHtmlAndHeadTags = originalHtml.substr(originalHtml.search(/\<body.*\>/))
    // remove body tag
    htmlWithoutHtmlAndHeadTags = htmlWithoutHtmlAndHeadTags.substr(originalHtml.match(/\<body.*\>/)[0].length)

    originalHtml = {
        value: originalHtml,
        origin: new Origin({
            action: "Initial Page HTML",
            inputValues: [],
            value: originalHtml,
            isHTMLFileContent: {
                filename: document.getElementById("fromjs-initial-html").getAttribute("html-filename")
            }
        })
    }
    mapInnerHTMLAssignment(document.body, originalHtml, "Initial Body HTML",  htmlWithoutHtmlAndHeadTags.length - originalHtml.value.length)


    //processNode(document.body, originalHtml)
}
//
// function processNode(el, originalHtml){
//     var isTextNode = el.innerHTML === undefined
//     if (isTextNode) {
//         addElOrigin(el, "textValue", {
//             "action": "initial html",
//             inputValues: [stringTrace(originalHtml)],
//             value: el.textContent,
//             inputValuesCharacterIndex: [0]
//         })
//
//     } else {
//         var children = Array.prototype.slice.apply(el.childNodes, [])
//
//         addElOrigin(el, "replaceContents", {
//             action: "initial html",
//             children: children
//         })
//
//         addElOrigin(el, "tagName", {
//             action: "initial html",
//             value: el.tagName,
//             inputValues: [stringTrace(originalHtml)],
//             inputValuesCharacterIndex: [0]
//         })
//
//         for (var i = 0;i<el.attributes.length;i++) {
//             var attr = el.attributes[i]
//             addElOrigin(el, "attribute_" + attr.name, {
//                 action: "initial html",
//                 value: " " + attr.name + "='" + attr.textContent + "'",
//                 inputValues: [stringTrace(originalHtml)],
//                 inputValuesCharacterIndex: [0]
//             })
//         }
//
//
//         children.forEach(function(child){
//             processNode(child, originalHtml)
//         })
//     }
// }

@@ -347,34 +347,19 @@
 var 
-htmlWithoutHtmlAndHeadTags
+bodyContent
  = o
@@ -443,113 +443,186 @@
     
-htmlWithoutHtmlAndHeadTags = htmlWithoutHtmlAndHeadTags.substr(originalHtml.match(/%5C%3Cbody.*%5C%3E/)%5B0%5D.length
+bodyContent = bodyContent.substr(originalHtml.match(/%5C%3Cbody.*%5C%3E/)%5B0%5D.length)%0A    // remove closing body tag%0A    bodyContent = bodyContent.substr(0, bodyContent.indexOf(%22%3C/body%3E%22)
 )%0A%0A 
@@ -1051,34 +1051,19 @@
 %22,  
-htmlWithoutHtmlAndHeadTags
+bodyContent
 .len

Cut out </body></html> from body content.