// Generated by CoffeeScript 1.12.7
var Easydb4MigrationPlugin,
  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 = (function(superClass) {
  extend(Easydb4MigrationPlugin, superClass);

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

  Easydb4MigrationPlugin.prototype.initOpts = function() {
    Easydb4MigrationPlugin.__super__.initOpts.call(this);
    return this.addOpts({
      migration: {
        check: Easydb4Migration,
        mandatory: true
      }
    });
  };

  Easydb4MigrationPlugin.prototype.name = function() {
    return "default";
  };

  Easydb4MigrationPlugin.prototype.init = function() {};

  Easydb4MigrationPlugin.prototype.payload = function(filename, json, update, store_file) {
    return console.info("Plugin:", this.name(), filename, json, update);
  };

  Easydb4MigrationPlugin.prototype.splitByType = function(filename, json, update, store_file, tn, col, obj_type_map, obj_type_func) {
    var _obj_type, filename_payload, i, j, len, len1, new_obj, obj, obj_type, obj_types, payload, payloads, ref, typ;
    if (obj_type_map == null) {
      obj_type_map = null;
    }
    if (obj_type_func == null) {
      obj_type_func = null;
    }
    payloads = {};
    ref = json.objects;
    for (i = 0, len = ref.length; i < len; i++) {
      obj = ref[i];
      typ = obj[tn][col];
      if (CUI.util.isEmpty(typ)) {
        typ = "undefined";
      }
      obj_type = tn + "_" + typ;
      if (obj_type_map) {
        obj_type = obj_type_map[obj_type];
      }
      if (obj_type_func) {
        obj_type = obj_type_func(obj);
      }
      if (!obj_type) {
        this._migration.logwarn("Object type '" + tn + "_" + typ + "' not found in mapping. Setting type to 'unknown'.");
        typ = "unknown";
        obj_type = tn + "_unknown";
      }
      if (CUI.util.isArray(obj_type)) {
        obj_types = obj_type;
      } else {
        obj_types = [obj_type];
      }
      for (j = 0, len1 = obj_types.length; j < len1; j++) {
        _obj_type = obj_types[j];
        if (!payloads[_obj_type]) {
          payloads[_obj_type] = {
            import_type: "db",
            objecttype: _obj_type,
            objects: []
          };
        }
        new_obj = {
          _mask: "_all_fields",
          _objecttype: _obj_type
        };
        new_obj[_obj_type] = CUI.util.copyObject(obj[tn], true);
        if (typ === "unknown") {
          new_obj[_obj_type].__unknown_type = obj[tn][col];
        }
        delete new_obj[_obj_type][col];
        payloads[_obj_type].objects.push(new_obj);
      }
    }
    for (typ in payloads) {
      payload = payloads[typ];
      filename_payload = ez5.bareBasename(filename) + "_" + typ + ".json";
      store_file(filename_payload, payload);
      console.debug(typ, payload, filename_payload);
    }
    return CUI.resolvedPromise({
      dont_store: true
    });
  };

  return Easydb4MigrationPlugin;

})(CUI.Plugin);
