// Generated by CoffeeScript 1.12.7
var CustomBaseConfigGazetteer,
  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;

CustomBaseConfigGazetteer = (function(superClass) {
  extend(CustomBaseConfigGazetteer, superClass);

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

  CustomBaseConfigGazetteer.prototype.getFieldDefFromParm = function(baseConfig, fieldName, def) {
    var field, getMask, getTopLevelRequiredFields;
    getMask = function(idTable) {
      if (CUI.util.isString(idTable)) {
        return false;
      }
      return Mask.getMaskByMaskName("_all_fields", idTable);
    };
    getTopLevelRequiredFields = function(objecttype) {
      return objecttype.getFields().filter(function(field) {
        return field.isTopLevelField() && field.isRequired();
      });
    };
    switch (def.plugin_type) {
      case "objecttype":
        field = new ez5.ObjecttypeSelector({
          form: {
            label: $$("custom.data.type.gazetteer.config.objecttype.label")
          },
          name: fieldName,
          show_name: true,
          store_value: "fullname",
          filter: function(objecttype) {
            var hasGazetteerField, mask, requiredUniqueFields;
            if (!objecttype.isHierarchy()) {
              return false;
            }
            mask = getMask(objecttype.table.id());
            if (!mask) {
              return false;
            }
            objecttype.addMask(mask);
            hasGazetteerField = objecttype.getFields().some(function(field) {
              return field instanceof CustomDataTypeGazetteer;
            });
            if (!hasGazetteerField) {
              return false;
            }
            requiredUniqueFields = getTopLevelRequiredFields(objecttype);
            if (requiredUniqueFields.length === 0) {
              return true;
            }
            if (requiredUniqueFields.length > 1) {
              return false;
            }
            return requiredUniqueFields[0] instanceof CustomDataTypeGazetteer;
          }
        });
        break;
      case "field_from":
        field = new ez5.FieldSelector({
          form: {
            label: $$("custom.data.type.gazetteer.config.field_from.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          placeholder: $$("custom.data.type.gazetteer.config.field-from-empty-text"),
          filter: function(field) {
            return field instanceof TextColumn && !(field instanceof NestedTable) && !(field instanceof NumberColumn) && !(field instanceof LocaTextColumn) && !field.isTopLevelField() && !field.insideNested();
          }
        });
        break;
      case "field_to":
        field = new ez5.FieldSelector({
          form: {
            label: $$("custom.data.type.gazetteer.config.field_to.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          filter: (function(_this) {
            return function(fieldTo) {
              var mask, objecttype, requiredUniqueFields;
              if (!(fieldTo instanceof CustomDataTypeGazetteer)) {
                return false;
              }
              if (fieldTo.insideNested()) {
                return false;
              }
              mask = getMask(fieldTo.table.id());
              if (!mask) {
                return false;
              }
              objecttype = new Objecttype(mask);
              requiredUniqueFields = getTopLevelRequiredFields(objecttype);
              if (requiredUniqueFields.length === 0) {
                return true;
              }
              return requiredUniqueFields[0].id() === fieldTo.id();
            };
          })(this)
        });
    }
    return field;
  };

  return CustomBaseConfigGazetteer;

})(BaseConfigPlugin);

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