text: "use strict";
  /*
  * indicative-rules
  *
  * (c) Harminder Virk <virk@adonisjs.com>
  *
  * For the full copyright and license information, please view the LICENSE
  * file that was distributed with this source code.
  */
  Object.defineProperty(exports, "__esModule", { value: true });
  const indicative_utils_1 = require("indicative-utils");
  const same_1 = require("../../raw/same");
  function castType(input, type) {
      if (type === 'string') {
          return indicative_utils_1.cast(input, 'string');
      }
      if (type === 'number') {
          return indicative_utils_1.cast(input, 'integer');
      }
      return input;
  }
  /**
   * Checks whether the field has been confirmed with same value or not using
   * the `_confirmation` convention. For example:
   *
   * The `password_confirmation` must exist and have the same value as the
   * `password` field.
   *
   * ```ts
   * import { validations } from 'indicative/validator'
   *
   * const rules = {
   *   password: 'confirmed'
   * }
   *
   * // or
   * const rules = {
   *   password: [
   *     validations.confirmed()
   *   ]
   * }
   * ```
   */
  const validation = {
      async: false,
      validate: (data, field, _args, config) => {
          const fieldValue = indicative_utils_1.getValue(data, field);
          if (indicative_utils_1.skippable(fieldValue, field, config)) {
              return true;
          }
          const valueType = typeof (fieldValue);
          let comparisonValue = indicative_utils_1.getValue(data, `${field}_confirmation`);
          /**
           * Cast type of comparison value when types are different. This is
           * important since the end-user has no way to cast type of the
           * confirmed field. For example:
           *
           * 1. Add `number` rule to the `pincode` field, which will cast the
           *    pincode to `number`.
           * 2. Also add `confirmed` rule to `pincode`, now you cannot cast
           *    it's value, so we need to do type conversion here.
           */
          /* eslint "valid-typeof": "off" */
          if (typeof (comparisonValue) !== valueType) {
              comparisonValue = castType(comparisonValue, valueType);
          }
          return same_1.same(fieldValue, comparisonValue);
      },
  };
  exports.default = validation;
  