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

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

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

  AutoKeyworder.prototype.getFieldDefFromParm = function(baseConfig, fieldName, def) {
    var field, i, len, options, ref, ref1, ref2, sort, version;
    sort = function(a, b) {
      return a.text.localeCompare(b.text);
    };
    switch (def.plugin_type) {
      case "auto_keyworder_objecttype":
        field = new ez5.ObjecttypeSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.objecttype.label")
          },
          name: fieldName,
          show_name: true,
          store_value: "fullname",
          filter: (function(_this) {
            return function(objecttype) {
              var data, id, mask;
              id = objecttype.table.id();
              if (CUI.util.isString(id)) {
                return false;
              }
              mask = Mask.getMaskByMaskName("_all_fields", objecttype.table.id());
              if (!mask) {
                return false;
              }
              objecttype.addMask(mask);
              data = {
                objecttype: objecttype.table.name()
              };
              if (!objecttype.getFields().some(function(field) {
                return _this.__filterByAssetField(field, data);
              })) {
                return false;
              }
              if (!objecttype.getFields().some(function(field) {
                return _this.__filterByDateTimeField(field, data);
              })) {
                return false;
              }
              if (objecttype.getFields().some(function(field) {
                return _this.__filterByTextField(field, data);
              })) {
                return true;
              }
              if (objecttype.getFields().some(function(field) {
                return _this.__filterByKeywordField(field, data);
              })) {
                return true;
              }
              return false;
            };
          })(this)
        });
        break;
      case "auto_keyworder_asset_version":
        options = [
          {
            text: $$("server.config.parameter.system.auto_keyworder.asset_version.placeholder"),
            value: null,
            disabled: true
          }, {
            value: "original"
          }
        ];
        ref2 = (ref = ez5.session.config.eas) != null ? (ref1 = ref.image) != null ? ref1.versions : void 0 : void 0;
        for (i = 0, len = ref2.length; i < len; i++) {
          version = ref2[i];
          options.push({
            value: version.version
          });
        }
        field = new CUI.Select({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.asset_version.label")
          },
          name: fieldName,
          options: options
        });
        break;
      case "auto_keyworder_asset_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.asset_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByAssetField,
          sort: sort
        });
        break;
      case "auto_keyworder_timestamp_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.timestamp_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByDateTimeField,
          sort: sort
        });
        break;
      case "cloudsight_subject_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.subject_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByTextField,
          sort: sort
        });
        break;
      case "cloudsight_keyword_categories_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.categories_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "cloudsight_keyword_similar_objects_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.similar_objects_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "cloudsight_keyword_quantity_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.quantity_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "clousight_keyword_gender_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.gender_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "clousight_keyword_material_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.material_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "cloudsight_keyword_color_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_cloudsight.color_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "imagga_tag_target_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_imagga.target_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
        break;
      case "deepva_type_target_field":
        field = new ez5.FieldSelector({
          form: {
            label: $$("server.config.parameter.system.auto_keyworder.configs_deepva.target_field.label")
          },
          name: fieldName,
          objecttype_data_key: "objecttype",
          store_value: "fullname",
          show_name: true,
          deep_linked_objects: true,
          placeholder: $$("server.config.parameter.system.auto_keyworder.no_field_selected"),
          filter: this.__filterByKeywordField,
          sort: sort
        });
    }
    return field;
  };

  AutoKeyworder.prototype.__filterByAssetField = function(field, data) {
    if (field.table.name() !== data.objecttype) {
      return false;
    }
    if (field.insideNested()) {
      return false;
    }
    return field.__cls === "ez5-eas-column";
  };

  AutoKeyworder.prototype.__filterByTextField = function(field, data) {
    if (field.table.name() !== data.objecttype) {
      return false;
    }
    if (field.insideNested()) {
      return false;
    }
    return field.__cls === "ez5-text-column";
  };

  AutoKeyworder.prototype.__filterByDateTimeField = function(field, data) {
    if (field.table.name() !== data.objecttype) {
      return false;
    }
    if (field.insideNested()) {
      return false;
    }
    return field.__cls === "ez5-date-time-column";
  };

  AutoKeyworder.prototype.__filterByKeywordField = function(field, data) {
    if (field instanceof MaskSplitter) {
      return false;
    }
    if (field.isSystemField()) {
      return false;
    }
    if (field.isTopLevelField()) {
      return false;
    }
    if (!field instanceof TextColumn) {
      return false;
    }
    if (field instanceof NumberColumn) {
      return false;
    }
    if (field instanceof EASColumn) {
      return false;
    }
    if (field instanceof DateColumn) {
      return false;
    }
    if (field instanceof BooleanColumn) {
      return false;
    }
    return true;
  };

  return AutoKeyworder;

})(BaseConfigPlugin);

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