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

ez5.CustomBaseConfigIUCN = (function(superClass) {
  extend(CustomBaseConfigIUCN, superClass);

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

  CustomBaseConfigIUCN.prototype.getFieldDefFromParm = function(baseConfig, fieldName, def) {
    var field, i, j, len, len1, options, ref, ref1, tag, tagGroup;
    switch (def.plugin_type) {
      case 'iucn_tag':
        options = [
          {
            text: $$("server.config.parameter.system.iucn_settings.tag.placeholder." + fieldName),
            value: null
          }
        ];
        ref = ez5.tagForm.tagGroups;
        for (i = 0, len = ref.length; i < len; i++) {
          tagGroup = ref[i];
          options.push({
            label: tagGroup.getDisplayName()
          });
          ref1 = tagGroup.getTags();
          for (j = 0, len1 = ref1.length; j < len1; j++) {
            tag = ref1[j];
            options.push({
              text: tag.getDisplayName(),
              value: tag.getId()
            });
          }
        }
        field = {
          type: CUI.Select,
          name: fieldName,
          options: options
        };
        break;
      case 'iucn_field_name':
        options = this.__searchInAllObjecttypes();
        field = {
          type: CUI.Select,
          name: fieldName,
          options: options
        };
    }
    return field;
  };

  CustomBaseConfigIUCN.prototype.__searchInAllObjecttypes = function() {
    var _, _options, addField, getFields, getLinkedFields, objecttype, options, optionsByObjecttype, ref;
    optionsByObjecttype = {};
    addField = function(tableName, field, path, fieldPath) {
      var label, value;
      if (path == null) {
        path = "";
      }
      if (fieldPath == null) {
        fieldPath = [];
      }
      if (!(field instanceof CustomDataTypeIUCN)) {
        return;
      }
      value = path + field.fullName();
      if (optionsByObjecttype[tableName].some(function(option) {
        return option.value === value;
      })) {
        return;
      }
      fieldPath.push(field);
      label = fieldPath.map(function(_field) {
        return _field.fullNameLocalized();
      }).join(" / ");
      return optionsByObjecttype[tableName].push({
        text: label,
        value: value
      });
    };
    getLinkedFields = function(linkedField) {
      var idTable, mask, path, tableName;
      idTable = linkedField.linkMask().table.id();
      path = linkedField.fullName() + ez5.IUCNUtil.LINK_FIELD_SEPARATOR;
      tableName = path.split(".")[0];
      mask = Mask.getMaskByMaskName("_all_fields", idTable);
      mask.invokeOnFields("all", true, ((function(_this) {
        return function(field) {
          return addField(tableName, field, path, [linkedField]);
        };
      })(this)));
    };
    getFields = function(idTable) {
      var mask, tableName, tableNameLocalized;
      mask = Mask.getMaskByMaskName("_all_fields", idTable);
      if (!mask.hasTags()) {
        return;
      }
      tableName = mask.table.name();
      tableNameLocalized = mask.table.nameLocalized();
      if (!optionsByObjecttype[tableName]) {
        optionsByObjecttype[tableName] = [
          {
            label: tableNameLocalized
          }
        ];
      }
      return mask.invokeOnFields("all", true, ((function(_this) {
        return function(field) {
          var _field, i, len, ref;
          if (field instanceof MaskSplitter) {
            return;
          }
          if (field.isTopLevelField() || field.isSystemField()) {
            return;
          }
          if (field instanceof LinkedObject) {
            if (field.table.id() === field.linkMask().table.id()) {
              return;
            }
            getLinkedFields(field);
            return;
          } else if (field instanceof ReverseLinkedTable) {
            ref = field.getFields("all");
            for (i = 0, len = ref.length; i < len; i++) {
              _field = ref[i];
              if (!(_field instanceof LinkedObject)) {
                addField(tableName, _field);
                continue;
              }
              getLinkedFields(_field);
            }
            return;
          }
          addField(tableName, field);
        };
      })(this)));
    };
    ref = ez5.schema.CURRENT._objecttype_by_name;
    for (_ in ref) {
      objecttype = ref[_];
      if (objecttype.name.indexOf('@') > -1) {
        continue;
      }
      getFields(objecttype.table_id);
    }
    options = [
      {
        text: $$("server.config.parameter.system.iucn_settings.iucn_fields.placeholder"),
        value: null
      }
    ];
    for (_ in optionsByObjecttype) {
      _options = optionsByObjecttype[_];
      if (_options.length === 1) {
        continue;
      }
      options = options.concat(_options);
    }
    return options;
  };

  return CustomBaseConfigIUCN;

})(BaseConfigPlugin);

ez5.session_ready((function(_this) {
  return function() {
    return BaseConfig.registerPlugin(new ez5.CustomBaseConfigIUCN());
  };
})(this));
