// Generated by CoffeeScript 1.12.7
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  hasProp = {}.hasOwnProperty;

Easydb4MigrationPlugin.UniGoettingen = (function(superClass) {
  extend(UniGoettingen, superClass);

  function UniGoettingen() {
    return UniGoettingen.__super__.constructor.apply(this, arguments);
  }

  UniGoettingen.prototype.name = function() {
    return "uni-goettingen";
  };

  UniGoettingen.prototype.payload = function(filename, json, update, store_file) {
    var _obj, beginn_map, ende_map, i, j, k, l, len, len1, len2, len3, map, name, nested, obj, ref, ref1, ref2, ref3, zeitpunkt_map;
    beginn_map = {
      1: "frühestens",
      2: "nach",
      3: "vor"
    };
    ende_map = {
      1: "spätestens",
      2: "nach",
      3: "vor"
    };
    zeitpunkt_map = {
      1: "um",
      2: "ca."
    };
    console.debug("post process", json);
    if (json.objecttype === "bilder") {
      ref = json.objects;
      for (i = 0, len = ref.length; i < len; i++) {
        _obj = ref[i];
        obj = _obj.bilder;
        if (obj["_nested:bilder__datierung_zeitpunkt"]) {
          CUI.util.removeFromArray(null, obj["_nested:bilder__datierung_zeitpunkt"], function(d) {
            return d.typ !== "1";
          });
          ref1 = obj["_nested:bilder__datierung_zeitpunkt"];
          for (j = 0, len1 = ref1.length; j < len1; j++) {
            nested = ref1[j];
            console.debug("attr komplett:", nested);
            if (!nested.attr_komplett) {
              continue;
            }
            name = nested.attr_komplett.attr_datierung_zeitpunkt["lookup:_id"].name;
            if (!zeitpunkt_map[name]) {
              delete nested.attr_komplett;
            } else {
              nested.attr_komplett.attr_datierung_zeitpunkt["lookup:_id"].name = zeitpunkt_map[name];
            }
          }
        }
        if (obj["_nested:bilder__datierung_spanne"]) {
          CUI.util.removeFromArray(null, obj["_nested:bilder__datierung_spanne"], function(d) {
            return d.typ !== "2";
          });
          ref2 = obj["_nested:bilder__datierung_spanne"];
          for (k = 0, len2 = ref2.length; k < len2; k++) {
            nested = ref2[k];
            console.debug("spanne beginn | ende:", nested);
            if (nested.attr_beginn) {
              name = nested.attr_beginn.attr_datierung_spanne_beginn["lookup:_id"].name;
              nested.attr_beginn.attr_datierung_spanne_beginn["lookup:_id"].name = beginn_map[name];
            }
            if (nested.attr_ende2) {
              name = nested.attr_ende2.attr_datierung_spanne_ende["lookup:_id"].name;
              nested.attr_ende2.attr_datierung_spanne_ende["lookup:_id"].name = ende_map[name];
            }
          }
        }
        if (obj["_nested:bilder__datierung_jahrhundert"]) {
          CUI.util.removeFromArray(null, obj["_nested:bilder__datierung_jahrhundert"], function(d) {
            return d.typ !== "3";
          });
        }
      }
      return;
    }
    switch (json.objecttype) {
      case "attr_datierung_spanne_beginn":
        map = beginn_map;
        break;
      case "attr_datierung_spanne_ende":
        map = ende_map;
        break;
      case "attr_datierung_zeitpunkt":
        map = zeitpunkt_map;
        break;
      default:
        return;
    }
    ref3 = json.objects;
    for (l = 0, len3 = ref3.length; l < len3; l++) {
      obj = ref3[l];
      name = obj[json.objecttype].name;
      if (map[name]) {
        obj[json.objecttype].name = map[name];
      }
    }
  };

  return UniGoettingen;

})(Easydb4MigrationPlugin);

ez5.session_ready((function(_this) {
  return function() {
    return Easydb4Migration.plugins.registerPlugin(Easydb4MigrationPlugin.UniGoettingen);
  };
})(this));
