"use strict";


/**
 * Return a new roiMAP changed with the fusion of certain ROIs.
 * @param {object} [options]
 * @param {string} [options.algorithm='commonBorderLength'] ; algorithm used to decide which ROIs are merged.
 * @param {number} [options.minCommonBorderLength=5] is an integer, determine the strength of the merging.
 * @param {number} [options.maxCommonBorderLength=5] is an integer, determine the strength of the merging.
 *
 * @return {this}
 */


export default function mergeRoi(roiMap, options = {}) {
    let {
        algorithm = 'commonBorderLength',
        minCommonBorderLength = 5,
        maxCommonBorderLength = 100,
        minCommonBorderRatio = 0.3,
        maxCommonBorderRatio = 1
    } = options;

    let borderLengths = roiMap.commonBorderLength;

    algorithm = algorithm.toLowerCase();

    let currentPosition = 0;
    let oldToNew = {};

    for (let currentID of Object.keys(borderLengths)) {
        let currentInfo = borderLengths[currentID];
        switch (algorithm) {
            case 'commonborderlength':
                let neighbourIDs = Object.keys(currentInfo);
                for (let neighbourID of neighbourIDs) {
                    console.log(neighbourID, currentID);
                    if (neighbourID !== currentID) { // it is not myself ...
                        if (currentInfo[neighbourID]>=minCommonBorderLength && currentInfo[neighbourID]<=maxCommonBorderLength) {
                            let smallerID = Math.min(neighbourID, currentID);
                            let largerID = Math.max(neighbourID, currentID);
                            if (! oldToNew[smallerID]) {
                                oldToNew[smallerID]={};
                            }
                            oldToNew[smallerID][largerID]=true;
                            if (oldToNew[largerID]) { // need to put everything to smallerID and remove property
                                for (let id of Object.keys(oldToNew[largerID])) {
                                    oldToNew[smallerID][id]=true;
                                }
                                delete oldToNew[largerID];
                            }
                            console.log('Should merge');
                        }
                    }
                }
                break;
            case 'commonborderratio':

                break;
            default:
                throw Error('Unknown algorithm to merge roi: '+algorithm);
        }

        currentPosition++;
    }

    console.log(oldToNew);

    return roiMap;

}

function merge(common, id1, id2) {

}

@@ -848,16 +848,37 @@
 on = 0;%0A
+    let newMap = %7B%7D;%0A
     let 
@@ -1190,65 +1190,8 @@
 ) %7B%0A
-                    console.log(neighbourID, currentID);%0A
     
@@ -1389,24 +1389,325 @@
 erLength) %7B%0A
+                            let newNeighbourID = neighbourID;%0A                            if (oldToNew%5BneighbourID%5D) newNeighbourID=oldToNew%5BneighbourID%5D;%0A                            let newCurrentID = currentID;%0A                            if (oldToNew%5BcurrentID%5D) newCurrentID=oldToNew%5BcurrentID%5D;%0A%0A
             
@@ -1749,16 +1749,19 @@
 h.min(ne
+wNe
 ighbourI
@@ -1755,33 +1755,36 @@
 newNeighbourID, 
-c
+newC
 urrentID);%0A     
@@ -1832,16 +1832,19 @@
 h.max(ne
+wNe
 ighbourI
@@ -1846,17 +1846,20 @@
 bourID, 
-c
+newC
 urrentID
@@ -1853,32 +1853,33 @@
  newCurrentID);%0A
+%0A
                 
@@ -1896,24 +1896,22 @@
   if (! 
-oldToNew
+newMap
 %5Bsmaller
@@ -1945,32 +1945,30 @@
             
-oldToNew
+newMap
 %5BsmallerID%5D=
@@ -2025,32 +2025,30 @@
             
-oldToNew
+newMap
 %5BsmallerID%5D%5B
@@ -2083,36 +2083,32 @@
                 
-if (
 oldToNew%5BlargerI
@@ -2109,16 +2109,76 @@
 argerID%5D
+=smallerID;%0A                            if (newMap%5BlargerID%5D
 ) %7B // n
@@ -2291,24 +2291,22 @@
 ct.keys(
-oldToNew
+newMap
 %5BlargerI
@@ -2348,24 +2348,22 @@
         
-oldToNew
+newMap
 %5Bsmaller
@@ -2449,24 +2449,22 @@
  delete 
-oldToNew
+newMap
 %5BlargerI
@@ -2551,16 +2551,48 @@
 d merge'
+, neighbourID, currentID, newMap
 );%0A     
@@ -2894,24 +2894,22 @@
 ole.log(
-oldToNew
+newMap
 );%0A%0A    

work in progress for new merge of Roi