1 (function (callback) {
  2   if (typeof define === 'function' && define.amd) {
  3     define(['core/Core'], callback);
  4   }
  5   else {
  6     callback();
  7   }
  8 }(function () {
  9 
 10 /**
 11  * Represents a Solr parameter.
 12  *
 13  * @param properties A map of fields to set. Refer to the list of public fields.
 14  * @class Parameter
 15  */
 16 AjaxSolr.Parameter = AjaxSolr.Class.extend(
 17   /** @lends AjaxSolr.Parameter.prototype */
 18   {
 19   /**
 20    * @param {Object} attributes
 21    * @param {String} attributes.name The parameter's name.
 22    * @param {String} [attributes.value] The parameter's value.
 23    * @param {Object} [attributes.local] The parameter's local parameters.
 24    */
 25   constructor: function (attributes) {
 26     AjaxSolr.extend(this, {
 27       name: null,
 28       value: null,
 29       locals: {}
 30     }, attributes);
 31   },
 32 
 33   /**
 34    * Returns the value. If called with an argument, sets the value.
 35    *
 36    * @param {String|Number|String[]|Number[]} [value] The value to set.
 37    * @returns The value.
 38    */
 39   val: function (value) {
 40     if (value === undefined) {
 41       return this.value;
 42     }
 43     else {
 44       this.value = value;
 45     }
 46   },
 47 
 48   /**
 49    * Returns the value of a local parameter. If called with a second argument,
 50    * sets the value of a local parameter.
 51    *
 52    * @param {String} name The name of the local parameter.
 53    * @param {String|Number|String[]|Number[]} [value] The value to set.
 54    * @returns The value.
 55    */
 56   local: function (name, value) {
 57     if (value === undefined) {
 58       return this.locals[name];
 59     }
 60     else {
 61       this.locals[name] = value;
 62     }
 63   },
 64 
 65   /**
 66    * Deletes a local parameter.
 67    *
 68    * @param {String} name The name of the local parameter.
 69    */
 70   remove: function (name) {
 71     delete this.locals[name];
 72   },
 73 
 74   /**
 75    * Returns the Solr parameter as a query string key-value pair.
 76    *
 77    * <p>IE6 calls the default toString() if you write <tt>store.toString()
 78    * </tt>. So, we need to choose another name for toString().</p>
 79    */
 80   string: function () {
 81     var pairs = [];
 82 
 83     for (var name in this.locals) {
 84       if (this.locals[name]) {
 85         pairs.push(name + '=' + encodeURIComponent(this.locals[name]));
 86       }
 87     }
 88 
 89     var prefix = pairs.length ? '{!' + pairs.join('%20') + '}' : '';
 90 
 91     if (this.value) {
 92       return this.name + '=' + prefix + this.valueString(this.value);
 93     }
 94     // For dismax request handlers, if the q parameter has local params, the
 95     // q parameter must be set to a non-empty value. In case the q parameter
 96     // has local params but is empty, use the q.alt parameter, which accepts
 97     // wildcards.
 98     else if (this.name == 'q' && prefix) {
 99       return 'q.alt=' + prefix + encodeURIComponent('*:*');
100     }
101     else {
102       return '';
103     }
104   },
105 
106   /**
107    * Parses a string formed by calling string().
108    *
109    * @param {String} str The string to parse.
110    */
111   parseString: function (str) {
112     var param = str.match(/^([^=]+)=(?:\{!([^\}]*)\})?(.*)$/);
113     if (param) {
114       var matches;
115 
116       while (matches = /([^\s=]+)=(\S*)/g.exec(decodeURIComponent(param[2]))) {
117         this.locals[matches[1]] = decodeURIComponent(matches[2]);
118         param[2] = param[2].replace(matches[0], ''); // Safari's exec seems not to do this on its own
119       }
120 
121       if (param[1] == 'q.alt') {
122         this.name = 'q';
123         // if q.alt is present, assume it is because q was empty, as above
124       }
125       else {
126         this.name = param[1];
127         this.value = this.parseValueString(param[3]);
128       }
129     }
130   },
131 
132   /**
133    * Returns the value as a URL-encoded string.
134    *
135    * @private
136    * @param {String|Number|String[]|Number[]} value The value.
137    * @returns {String} The URL-encoded string.
138    */
139   valueString: function (value) {
140     value = AjaxSolr.isArray(value) ? value.join(',') : value;
141     return encodeURIComponent(value);
142   },
143 
144   /**
145    * Parses a URL-encoded string to return the value.
146    *
147    * @private
148    * @param {String} str The URL-encoded string.
149    * @returns {Array} The value.
150    */
151   parseValueString: function (str) {
152     str = decodeURIComponent(str);
153     return str.indexOf(',') == -1 ? str : str.split(',');
154   }
155 });
156 
157 /**
158  * Escapes a value, to be used in, for example, an fq parameter. Surrounds
159  * strings containing spaces or colons in double quotes.
160  *
161  * @public
162  * @param {String|Number} value The value.
163  * @returns {String} The escaped value.
164  */
165 AjaxSolr.Parameter.escapeValue = function (value) {
166   // If the field value has a space, colon, quotation mark or forward slash
167   // in it, wrap it in quotes, unless it is a range query or it is already 
168   // wrapped in quotes.
169   if (value.match(/[ :\/"]/) && !value.match(/[\[\{]\S+ TO \S+[\]\}]/) && !value.match(/^["\(].*["\)]$/)) {
170     return '"' + value.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
171   }
172   return value;
173 }
174 
175 }));
176