"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getFieldProps = void 0;
var _ui = require("@mirai/ui");
var _react = _interopRequireDefault(require("react"));
var _testDNI = require("./testDNI");
var _helpers = require("../../helpers");
var _Checkout = require("../Checkout.constants");
var _Checkout2 = require("../Checkout.l10n");
var style = _interopRequireWildcard(require("../Checkout.module.css"));
const _excluded = ["input", "type"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
const {
  HTML,
  OPTION,
  PHONE,
  SELECT
} = _Checkout.INPUT;
const getFieldProps = function () {
  let field = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    {
      input,
      type
    } = _ref,
    props = _objectWithoutProperties(_ref, _excluded);
  let {
    value,
    error,
    language,
    otherConditions,
    session,
    translate = () => {},
    clubDiscount = 0,
    currency,
    currencyFormat
  } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  let {
    name = '',
    url: {
      conditions = {}
    } = {}
  } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  let onPress = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : () => {};
  const conditionsUrl = conditions[language] || (conditions === null || conditions === void 0 ? void 0 : conditions.en);
  const labelKey = field === 'joinClub' ? clubDiscount > 0 ? _Checkout2.L10N.LABEL_JOINCLUB_WITH_DISCOUNT : _Checkout2.L10N.LABEL_JOINCLUB_BENEFITS : _Checkout2.L10N["LABEL_".concat(field.toUpperCase())];
  return _objectSpread(_objectSpread({}, props), {}, {
    checked: input === OPTION ? (value !== undefined ? value : props.checked) || false : undefined,
    children: input === HTML && props.children ? (0, _helpers.parseHtml)(props.children, {
      small: true
    }) : undefined,
    disabled: field === 'email' && session || undefined,
    error: field !== 'phone' ? (value === null || value === void 0 ? void 0 : value.length) > 0 && !!error : (value === null || value === void 0 ? void 0 : value.length) > 5 && !!error,
    hint: translate(_Checkout2.L10N["".concat(field.toUpperCase(), "_HINT")]),
    id: field ? "checkout-".concat(field.replace(/\./g, '-')) : undefined,
    key: field,
    label: props.label || translate(labelKey, {
      club: name,
      clubLink: /*#__PURE__*/_react.default.createElement(_ui.Action, {
        href: conditionsUrl,
        small: true,
        target: "_blank",
        underline: true,
        style: {
          display: 'inline'
        }
      }, translate(_Checkout2.L10N.LABEL_TERMS_AND_CONDITIONS)),
      discount: currencyFormat ? currencyFormat({
        currency,
        value: clubDiscount
      }) : clubDiscount,
      otherConditions: !!otherConditions,
      termsLink: /*#__PURE__*/_react.default.createElement(_ui.Action, {
        small: true,
        onPress: onPress,
        style: {
          display: 'inline'
        }
      }, translate(_Checkout2.L10N.ACTION_MORE_INFO))
    }) || field,
    labelPrefix: field === 'phone' ? translate(_Checkout2.L10N.LABEL_PHONE_PREFIX) : undefined,
    name: field,
    requiredText: translate(_Checkout2.L10N.LABEL_REQUIRED),
    showRequired: true,
    small: [OPTION, HTML].includes(input),
    success: field !== 'phone' ? !!value && !error : (value === null || value === void 0 ? void 0 : value.length) > 4 && !error,
    test: field === 'dni' ? _testDNI.testDNI : undefined,
    type: type === _Checkout.INPUT_TYPE.NUMBER ? 'number' : type === _Checkout.INPUT_TYPE.EMAIL ? 'email' : input === PHONE ? 'inputPhone' : input === OPTION ? 'checkbox' : undefined,
    value: input === SELECT && value === undefined ? props.options[0] : value || props.value,
    className: style.wide
  });
};
exports.getFieldProps = getFieldProps;
//# sourceMappingURL=getFieldProps.js.map