// 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;

Easydb4Migration.SyrianHeritage = (function(superClass) {
  extend(SyrianHeritage, superClass);

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

  SyrianHeritage.prototype.name = function() {
    return "syrian-heritage";
  };

  SyrianHeritage.prototype.payload = function(filename, json, update) {
    return this._migration.query('SELECT * FROM "source.public.externe_quellen"').done((function(_this) {
      return function(result) {
        var ext_by_id, i, len, ref, repl_ext_quelle, row;
        ext_by_id = {};
        ref = result.rows;
        for (i = 0, len = ref.length; i < len; i++) {
          row = ref[i];
          ext_by_id["externe_quellen:" + row.id] = row;
        }
        repl_ext_quelle = function(data) {
          var _url, ext_quelle, hostnameParts, item, j, k, len1, location, ref1, url, v;
          for (k in data) {
            v = data[k];
            if (CUI.util.isArray(v)) {
              for (j = 0, len1 = v.length; j < len1; j++) {
                item = v[j];
                repl_ext_quelle(item);
              }
              continue;
            }
            if (CUI.util.isPlainObject(v)) {
              repl_ext_quelle(v);
              continue;
            }
            if (k.endsWith(":source_name") && v === "externe_quellen") {
              ext_quelle = ext_by_id[data.lk_externe_quelle_id.externe_quellen["lookup:_id"].easydb4_reference];
              if (ext_quelle.url.endsWith("/")) {
                if ((ref1 = data.name) != null ? ref1.startsWith("/") : void 0) {
                  _url = ext_quelle.url + data.name.substr(1);
                } else {
                  _url = ext_quelle.url + (data.name || "");
                }
              } else {
                _url = ext_quelle.url + "/" + (data.name || "");
              }
              url = {
                url: _url,
                text_plain: ext_quelle.name,
                _standard: {}
              };
              data.url = url;
              url._standard.text = url.text || url.url;
              url._fulltext = {
                text: ext_quelle.name,
                string: data.url.url
              };
              location = CUI.parseLocation(url.url);
              if (location) {
                hostnameParts = location.hostname.split(".");
                url.tld = hostnameParts[hostnameParts.length - 1];
                url.hostname = location.hostname;
              } else {
                url.tld = "";
                url.hostname = "";
              }
            }
          }
        };
        return repl_ext_quelle(json);
      };
    })(this));
  };

  return SyrianHeritage;

})(Easydb4MigrationPlugin);

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