import { Make } from '../af/util/make.js';
import { parseExpression } from './Parser.js';
import Binding from './Binding.js';
import BindingRegistry from './BindingRegistry.js';

let IfBinding = Make(/** @lends module:DataBinding.IfBinding# **/{

    /** @type {string} */
    name: 'bind-if',

    /**
     * @type {Node}
     */
    parentNode: null,

    /**
     * @type {Node}
     */
    node: null,

    /**
     * @type {Node}
     */
    nextSibling: null,

    /**
     *
     * @constructs
     * @extends {Binding}
     * @param {Node} parentNode - this node
     * @param {string} text - the attribute value
     * @param {ScopeInfo} scopeInfo - bindings container
     *
     * @return {void}
     */
    _make: function({ parentNode, text, scopeInfo }) {
        this.node = parentNode;
        this.parentNode = this.node.parentNode;
        this.text = text;
        this.nextSibling = parentNode.nextSibling;

        scopeInfo.bindings.push(this);
    },

    update: function(scope) {
        let isTrue = parseExpression(this.text, scope);

        if (isTrue) {
            if (this.node.parentNode !== this.parentNode) {
                if (this.nextSibling) {
                    this.parentNode.insertBefore(this.node, this.nextSibling);
                } else {
                    this.parentNode.appendChild(this.node);
                }
            }
        } else {
            if (this.node.parentNode === this.parentNode) {
                this.parentNode.removeChild(this.node);
            }
        }
    },

}, Binding).get();

BindingRegistry.register(IfBinding);

export default IfBinding;

@@ -440,27 +440,23 @@
  */%0A    
-nextSibling
+comment
 : null,%0A
@@ -808,272 +808,303 @@
 his.
-parentNode = this.node.parentNode;%0A        this.text = text;%0A        this.nextSibling = parentNode.nextSibling;%0A%0A        scopeInfo.bindings.push(this);%0A    %7D,%0A%0A    update: function(scope) %7B%0A        let isTrue = parseExpression(this.text, scope);%0A%0A        if (isTru
+text = text;%0A        this.comment = document.createComment(%60   If: $%7Btext%7D   %60);%0A%0A        scopeInfo.bindings.push(this);%0A    %7D,%0A%0A    update: function(scope) %7B%0A        if (this.comment.parentNode) %7B%0A            this.parentNode = this.comment.parentNode;%0A        %7D%0A%0A        if (this.node.parentNod
 e) %7B
@@ -1112,38 +1112,29 @@
             
-if (
 this.
-node.
 parentNode !
@@ -1136,17 +1136,20 @@
 ode 
-!=
 = this.
+node.
 pare
@@ -1146,35 +1146,33 @@
 .node.parentNode
-) %7B
+;
 %0A               
@@ -1156,32 +1156,35 @@
 ntNode;%0A        
+%7D%0A%0A
         if (this
@@ -1188,110 +1188,149 @@
 his.
-nextSibling) %7B%0A                    this.parentNode.insertBefore(this.node, this.nextSibling
+parentNode.nodeType === 11) %7B%0A            return scope.update();%0A        %7D%0A%0A        let isTrue = parseExpression(this.text, scope
 );%0A
+%0A
         
     
@@ -1325,37 +1325,86 @@
         
-        %7D else %7B%0A    
+if (isTrue) %7B%0A            if (!this.parentNode.contains(this.node)) %7B%0A
         
@@ -1431,14 +1431,15 @@
 ode.
-append
+replace
 Chil
@@ -1453,28 +1453,24 @@
 node
-);%0A                %7D
+, this.comment);
 %0A   
@@ -1510,37 +1510,32 @@
        if (this.
-node.
 parentNode === t
@@ -1532,29 +1532,29 @@
 Node
- === this.parentN
+.contains(this.n
 ode)
+)
  %7B%0A 
@@ -1590,18 +1590,33 @@
 e.re
-mov
+plac
 eChild(
+this.comment, 
 this

refactor IfBinding to work reliably