function EnumerateDeletedNodes(b,a,c){function e(l){c.Node(l,false,true)||f.push(l);l=a.Node(l);l.children&&l.children.forEach(function(m){e(m)})}var f=null;b.set.forEach(function(l){if(l.action=="delete"){l.deleted=[];f=l.deleted;e(l.nid)}})}function CleanDeletedNodes(b){b.set.forEach(function(a){a.action=="delete"&&delete a.deleted;delete a.fixed})}
function CleanServerMoves(b,a){b.set.forEach(function(c){if(c.action=="move"&&c.args.pnid==a.Node(c.nid).pnid){c.action="reorder";delete c.args.pnid}})}function CombinePrePost(b){b.set=b.pre.concat(b.set).concat(b.post);delete b.pre;delete b.post}
function OpenConflictDialog(b,a){var c=Cc["@mozilla.org/appshell/window-mediator;1"].getService().QueryInterface(Ci.nsIWindowMediator),e=c.getMostRecentWindow("foxmarks:progress");if(!e){e=c.getMostRecentWindow(null);if(!e)return null}c={};e.openDialog(b,"_blank","chrome,dialog,modal,centerscreen",c,a);return c.selection}
var DETECTORS={insert_insert:function(b,a,c,e){if(b.nid==a.nid){var f=[];forEach(b.args,function(m,p){a.args[p]&&!DETECTORS.HARMLESS_ATTRS[p]&&m!=a.args[p]&&f.push(p+":"+m)});forEach(a.args,function(m,p){b.args[p]&&!DETECTORS.HARMLESS_ATTRS[p]&&m!=b.args[p]&&f.push(p+":"+m)});if(f.length){var l=c.Node(b.nid);e=e.Node(a.nid);c=c.handleNidConflict(l,e,f);showedUI=true;if(c=="local")return["overwrite","local"];else if(c=="server")return["overwrite","server"];else if(c=="same")return["drop","both"];else throw 2;
}else return["drop","both"]}if(b.args.pnid==a.args.pnid&&b.args.bnid==a.args.bnid)return["transit","server"];return null},insert_move:function(b,a){if(b.args.pnid!=a.args.pnid&&b.args.bnid==a.nid)return["nextsib","left"];if(b.nid!=a.nid&&b.args.pnid==a.args.pnid&&b.args.bnid==a.args.bnid)return["transit","server"];return null},insert_reorder:function(b,a,c,e){if(b.args.pnid==e.Node(a.nid).pnid&&b.args.bnid==a.args.bnid&&!a.fixed)return["transit","server"];if(b.args.pnid==e.Node(a.nid).pnid&&b.args.bnid==
a.nid)return["nextsib","left"];return null},insert_update:function(){return null},delete_insert:function(b,a){if(b.deleted.indexOf(a.args.bnid)>=0)return["nextsib","right"];if(b.deleted.indexOf(a.args.pnid)>=0)return["delinsert","left"];return null},delete_delete:function(b,a){if(b.nid==a.nid)return["drop","both"];if(b.deleted.indexOf(a.nid)>=0)return["drop","right"];if(a.deleted.indexOf(b.nid)>=0)return["drop","left"];return null},delete_move:function(b,a){if(b.deleted.indexOf(a.args.bnid)>=0)return["nextsib",
"right"];if(b.deleted.indexOf(a.nid)>=0)return["delmoveout","left"];if(b.deleted.indexOf(a.args.pnid)>=0)return["delmovein","left"];return null},delete_reorder:function(b,a){if(b.deleted.indexOf(a.nid)>=0)return["drop","right"];if(b.nid==a.args.bnid)return["nextsib","right"];return null},delete_update:function(b,a){if(b.deleted.indexOf(a.nid)>=0){for(var c in a.args)if(a.args.hasOwnProperty(c)&&!DETECTORS.HARMLESS_ATTRS[c])return["delupdate","left"];return["drop","right"]}return null},move_move:function(b,
a){if(b.args.pnid==a.args.pnid)if(b.nid==a.nid)return b.args.bnid==a.args.bnid?["drop","both"]:["drop","server"];else{if(b.args.bnid==a.args.bnid)return["transit","server"]}else if(b.nid==a.nid){showedUI=true;return["drop","server"]}else if(b.nid==a.args.bnid)return["nextsib","right"];else if(b.args.bnid==a.nid)return["nextsib","left"];return null},move_reorder:function(b,a,c,e){if(b.nid==a.nid)return["drop","right"];if(b.nid==a.args.bnid&&b.args.pnid!=e.Node(a.nid).pnid&&!a.fixed)return["nextsib",
"right"];if(b.args.pnid==e.Node(a.nid).pnid&&b.args.bnid==a.args.bnid&&!a.fixed)return["transit","server"];return null},move_update:function(){return null},reorder_reorder:function(b,a,c,e){if(c.Node(b.nid).pnid!=e.Node(a.nid).pnid)return null;return b.nid==a.nid?b.args.bnid==a.args.bnid?b.fixed?null:["drop","both"]:["drop","server"]:b.nid==a.args.bnid&&!a.fixed?["commutereorder","left"]:b.args.bnid==a.nid&&!b.fixed?["commutereorder","right"]:null},reorder_update:function(){return null},HARMLESS_ATTRS:{created:true,
visited:true,modified:true,"private":true,icon:true},update_update:function(b,a,c,e){if(b.nid==a.nid){var f=[];forEach(b.args,function(l,m){a.args[m]&&!DETECTORS.HARMLESS_ATTRS[m]&&l!=a.args[m]&&f.push(m)});if(f.length==1&&f[0]=="tags")return["drop","server"];if(b.nid==NODE_ROOT)return["drop","server"];if(f.length){b=c.Node(b.nid);e=e.Node(a.nid);c=c.handleNidConflict(b,e,f);showedUI=true;if(c=="local")return["drop","server"];else if(c=="server")return["drop","local"];else if(c=="same")return["drop",
"both"];else throw 2;}return null}}};
function Synchronize(b,a,c,e,f,l){function m(i){switch(i){case "both":a.drop(h);c.drop(n);h-=1;q=true;break;case "local":a.drop(h);h-=1;q=true;break;case "server":c.drop(n);n-=1;break;default:throw Error("Unknown sync parameter "+g[1]);}}function p(i){var d=null,k=null;if(i=="server"){d=j;k=c}else{d=sc;k=a}i=NearestAncestor(d.args.pnid,b,e,f);d.args.pnid=i;d.args.bnid=null;k.pre.push(new Command("move",d.nid,{pnid:i}));d=new Command("reorder",d.nid,{bnid:null});a.post.push(d);c.post.push(d);h=-1;
q=true}function A(i){var d=null,k=null;if(i=="server"){d=j;k=c}else{d=sc;k=a}i=NearestAncestor(d.args.pnid,b,e,f);d.args.pnid=i;d.args.bnid=null;k.pre.push(new Command("move",d.nid,{pnid:i}));d=new Command("reorder",d.nid,{bnid:null});a.post.push(d);c.post.push(d);h=-1;q=true}function z(i){var d=null,k=null,o=null,s=null,t=0,u=0;if(i=="server"){d=j;k=a;updateNS=e;o=sc;s=f;delCS=c;t=n;u=h}else{d=sc;k=c;updateNS=f;o=j;s=e;delCS=a;t=h;u=n}i=ResurrectNodes(d.nid,b,updateNS,s,k,u,o,delCS,t,null);i!=updateNS.Node(d.nid).pnid&&
delCS.pre.push(new Command("move",d.nid,{pnid:i}));if(updateNS.OrderIsImportant()){d=new Command("reorder",d.nid,{bnid:null});a.post.push(d);c.post.push(d)}h=-1;q=true}function x(i){var d=null,k=null,o=null,s=null,t=null,u=null,B=null,C=0;if(i=="server"){d=j;k=a;o=h;s=e;t=sc;u=f;B=c;C=n}else{d=sc;k=c;o=n;s=f;t=j;u=e;B=a;C=h}k.set.splice(o,1);ResurrectNodes(d.nid,b,s,u,k,o,t,B,C,d.args.bnid);h=-1;q=true}function v(i){var d=i=="local"?sc:j,k=i=="local"?a.set:c.set,o=i=="local"?h:n;d.fixed=true;if((i==
"local"?e:f).Node(d.nid,false,true)==null)return m(i=="local"?"server":"local");Xmarks.LogWrite("Duplicating command from "+i+": "+JSON.stringify(d));for(o++;o<k.length;++o)if(!k[o].fixed)break;k.splice(o,0,d);h=-1;q=true}EnumerateDeletedNodes(a,b,e);EnumerateDeletedNodes(c,b,f);a.pre=[];a.post=[];c.pre=[];c.post=[];for(var w=0,r={},h=0;h<a.set.length;++h)for(var j=a.set[h],g=null,q=false,n=0;n<c.set.length;++n){sc=c.set[n];var y=DETECTORS[j.action+"_"+sc.action];if(y){try{g=y(j,sc,e,f,false)}catch(D){Xmarks.LogWrite("Synchronize: failed processing "+
JSON.stringify(j)+", "+JSON.stringify(sc));Xmarks.LogWrite("Error is "+JSON.stringify(D));throw D;}if(!g)continue;if(g[1]=="left")g[1]="local";else if(g[1]=="right")g[1]="server"}else{y=DETECTORS[sc.action+"_"+j.action];if(!y)throw Error("Couldn't find conflict detector for "+j.action+" : "+sc.action);try{g=y(sc,j,f,e,true)}catch(E){Xmarks.LogWrite("Synchronize: failed processing (reversed) "+JSON.stringify(sc)+", "+JSON.stringify(j));Xmarks.LogWrite("Error is "+JSON.stringify(E));throw E;}if(!g)continue;
if(g[1]=="left")g[1]="server";else if(g[1]=="right")g[1]="local"}r[g[0]]=(r[g[0]]||0)+1;Xmarks.LogWrite(">>> Sync: Executing "+g[0]+"("+g[1]+") on "+JSON.stringify(j)+" vs. "+JSON.stringify(sc));switch(g[0]){case "drop":m(g[1]);break;case "overwrite":switch(g[1]){case "local":c.drop(n);n-=1;a.set[h].action="update";a.set[h].args.bnid=undefined;a.set[h].args.pnid=undefined;break;case "server":a.drop(h);h-=1;c.set[n].action="update";c.set[n].args.bnid=undefined;c.set[n].args.pnid=undefined;break;default:throw Error("Unknown sync parameter "+
g[1]);}break;case "transit":switch(g[1]){case "local":j.args.bnid=sc.nid;h-=1;q=true;break;case "server":sc.args.bnid=j.nid;h=-1;q=true;break;default:throw Error("Uknown sync parameter "+g[1]);}break;case "nextsib":switch(g[1]){case "local":j.args.bnid=e.NextSibling(j.args.bnid,b);h-=1;q=true;break;case "server":sc.args.bnid=f.NextSibling(sc.args.bnid,b);h=-1;q=true;break;default:throw Error("Unknown sync parameter "+g[1]);}break;case "delinsert":p(g[1]);break;case "delmovein":A(g[1]);break;case "delmoveout":x(g[1]);
break;case "delupdate":z(g[1]);break;case "commutereorder":v(g[1]);break;default:throw Error("Unknown sync operator "+g[0]);}if(q){w+=1;if(w>=2E3)throw 1013;break}}CleanDeletedNodes(a);CleanDeletedNodes(c);CombinePrePost(a);CombinePrePost(c);CleanServerMoves(a,f);l(a,c,r,false)}
function NearestAncestor(b,a,c,e){for(;b;){var f=a.Node(b,false,true);if(!f)throw Error("NearestAncestor failed accessing "+b);if(c.Node(b,false,true)&&e.Node(b,false,true))return b;b=f.pnid}throw"Couldn't find a common ancestor!";}
function ResurrectNodes(b,a,c,e,f,l,m,p,A,z){for(var x=[b],v=null;x.length;){b=x.shift();var w=a.Node(b),r=c.Node(b,false,true),h=m.deleted.indexOf(b);if(h>=0&&r&&(!v||r.pnid==w.pnid)){r=r.GetSafeAttrs();if(!v){v=NearestAncestor(r.pnid,a,c,e);r.pnid=v;if(z)r.bnid=z}f.set.splice(l++,0,new Command("insert",b,r));forEach(f.set,function(j,g){j.action=="update"&&j.nid==b&&f.drop(g)});h>=0&&m.deleted.splice(h,1);w.children&&forEach(w.children,function(j){x.push(j)})}}m.deleted.length||p.drop(A);return v}
;
