// 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.MdcBerlin = (function(superClass) {
  extend(MdcBerlin, superClass);

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

  MdcBerlin.prototype.name = function() {
    return "mdc-berlin";
  };

  MdcBerlin.prototype.person_typ = {
    "typ_fotograf": ["Fotograf", "Photographer"]
  };

  MdcBerlin.prototype.payload = function(filename, json, update, store_file) {
    var _tags, add_tag, dfr, freigabestufen_tags, i, k, len, obj, ref, ref1, update_freigabestufe, v;
    if (json.import_type === "tags") {
      dfr = new CUI.Deferred();
      freigabestufen_tags = json.tags[0]._tags;
      update_freigabestufe = (function(_this) {
        return function(reference, displayname_de, displayname_en, desc_de, desc_en) {
          var i, len, tag;
          console.debug("update ref:", reference);
          for (i = 0, len = freigabestufen_tags.length; i < len; i++) {
            tag = freigabestufen_tags[i];
            console.debug("tag:", tag);
            if (tag.tag.reference === reference) {
              console.debug("yo:", tag);
              tag.tag.displayname = {
                "de-DE": displayname_de,
                "en-US": displayname_en
              };
              tag.tag.description = {
                "de-DE": desc_de,
                "en-US": desc_en
              };
            }
          }
        };
      })(this);
      add_tag = (function(_this) {
        return function(shortname, displayname_de, displayname_en, desc_de, desc_en) {
          return _tags.push({
            tag: {
              shortname: shortname,
              displayname: {
                "de-DE": displayname_de,
                "en-US": displayname_en
              },
              description: {
                "de-DE": desc_de,
                "en-US": desc_en
              },
              is_default: false,
              enabled: true,
              displaytype: "search",
              type: "individual"
            }
          });
        };
      })(this);
      _tags = [];
      json.tags.push({
        taggroup: {
          reference: "easydb4:persontyp",
          type: "checkbox",
          displayname: {
            "de-DE": "Personentyp"
          }
        },
        _tags: _tags
      });
      ref = this.person_typ;
      for (k in ref) {
        v = ref[k];
        add_tag(k, v[0], v[1], void 0, void 0);
      }
      this._migration.query('SELECT id, name, name_en, remark, remark_en FROM "source.public.freigabestufen" ORDER BY id').fail(dfr.reject).done((function(_this) {
        return function(result) {
          var i, len, ref1, row;
          console.debug("stufen", result);
          ref1 = result.rows;
          for (i = 0, len = ref1.length; i < len; i++) {
            row = ref1[i];
            update_freigabestufe("freigabestufen:" + row.id, row.name, row.name_en, row.remark, row.remark_en);
          }
          return dfr.resolve();
        };
      })(this));
      dfr.always((function(_this) {
        return function() {
          return console.debug("payload:", filename, json, update, store_file);
        };
      })(this));
      return dfr.promise();
    }
    if (json.objecttype === "person") {
      ref1 = json.objects;
      for (i = 0, len = ref1.length; i < len; i++) {
        obj = ref1[i];
        if (!obj._tags) {
          obj._tags = [];
        }
        if (obj.person.ist_fotograf) {
          obj._tags.push({
            "lookup:_id": {
              shortname: "typ_fotograf"
            }
          });
        }
      }
      return;
    }
  };

  return MdcBerlin;

})(Easydb4MigrationPlugin);

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