import { Color, pt } from "lively.graphics";
import { morph } from "lively.morphic";

export var migrations = [

  {
    date: "2017-04-08",
    name: "Text and Label textAndAttributes format change",
    description: `
Changing the format from
  [[string1, attr1_1, attr1_2], [string2, attr2_1, attr2_2], ...]
to
  [string1, attr1, string2, attr2, ...].
    `,
    snapshotConverter: idAndSnapshot => {
      let {snapshot} = idAndSnapshot;
      for (let key in snapshot) {
        let serialized = snapshot[key],
            textAndAttributes = serialized.props && serialized.props.textAndAttributes;
        if (!textAndAttributes) continue;
        let {value} = textAndAttributes;
        if (!Array.isArray(value)) {
          console.warn(`object migrator found textAndAttributes field but it is not an Array!`);
          continue;
        }
        if (!value.length || typeof value[0] === "string") continue; // OK
        // flatten values
        value = [].concat.apply([], value);
        for (let i = 0; i < value.length; i += 2) {
          let text = value[i], attr = value[i+1];
          if (attr && Array.isArray(attr)) // merge multi-attributes
            value[i+1] = Object.assign({}, ...attr);
        }
        serialized.props.textAndAttributes = {...textAndAttributes, value};
      }
      return idAndSnapshot;
    }
  },

  {
    date: "2017-04-29",
    name: "Window button fix",
    description: `
A recent change in the structure of windows, that now adds a "button wrapper"
morph breaks old windows without it.
`,
    objectConverter: ({snapshot, id}, pool) => {
      let rootMorph = pool.refForId(id).realObj;
      if (rootMorph && rootMorph.isMorph)
        rootMorph.withAllSubmorphsDo(win => {
          if (!win.isWindow || win.submorphs.some(m => m.name === "button wrapper")) return;

          let btnWrapper = morph({
            name: "button wrapper",
            styleClasses: ["buttonGroup"],
            submorphs: win.buttons()
          });
          win.controls = [btnWrapper, win.titleLabel()];
          if (win.resizable) win.controls.push(win.resizer());
          win.submorphs = [...win.submorphs, ...win.controls];
          btnWrapper.position = pt(0,0)
          win.buttons().forEach(ea => ea.extent = pt(14,14));
          win.getWindow().relayoutWindowControls();
        });
    }
  }

];
@@ -77,16 +77,171 @@
 orphic%22;
+%0Aimport %7B SnapshotInspector %7D from %22lively.serializer2/debugging.js%22;%0Aimport %7B removeUnreachableObjects %7D from %22lively.serializer2/snapshot-navigation.js%22;
 %0A%0Aexport
@@ -1502,16 +1502,17 @@
 %7D%0A  %7D,%0A%0A
+%0A
   %7B%0A    
@@ -2481,16 +2481,16 @@
 rols();%0A
-
         
@@ -2499,15 +2499,845 @@
 ;%0A    %7D%0A
+  %7D,%0A%0A%0A  %7B%0A    date: %222017-05-03%22,%0A    name: %22Style Sheet Status Fix%22,%0A    description: %60%0AState management of the style sheets has changes substantially, moving all of the style sheets that are being applied to the world.%0A%60,%0A    snapshotConverter: idAndSnapshot =%3E %7B%0A      let %7Bid: rootId, snapshot%7D = idAndSnapshot,%0A          i = SnapshotInspector.forSnapshot(snapshot);%0A      for (let id in snapshot) %7B%0A        let %7B props %7D = snapshot%5Bid%5D;%0A        if (!props %7C%7C !props.styleSheets) continue;%0A        if (!props.styleSheets.value) props.styleSheets.value = %5B%5D;%0A        props.styleSheets.value = props.styleSheets.value.filter(ea =%3E %7B%0A          let styleSheet = snapshot%5Bea.id%5D;%0A          return !!styleSheet.props.sizzle;%0A        %7D);%0A      %7D%0A      removeUnreachableObjects(%5BrootId%5D, snapshot);%0A      return idAndSnapshot;%0A    %7D%0A
   %7D%0A%0A%5D;

add object migration for morphs with old style sheets