"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactHookForm = require("react-hook-form");
var _reactNative = require("react-native");
var _theming = require("../../core/theming");
var _Button = _interopRequireDefault(require("../Button"));
var _Checkbox = _interopRequireDefault(require("../Checkbox/Checkbox"));
var _helpers = require("../Input/helpers");
var _TextInput = _interopRequireDefault(require("../Input/TextInput"));
var _Radio = _interopRequireDefault(require("../Radio"));
var _Select = _interopRequireDefault(require("../Select"));
var _Switch = _interopRequireDefault(require("../Switch/Switch"));
var _Text = _interopRequireDefault(require("../Typography/Text"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const ControlledTextInput = props => {
  const {
    validation,
    ...rest
  } = props;
  const {
    control
  } = (0, _reactHookForm.useFormContext)();
  const getKeyboardTypeFromType = type => {
    switch (type) {
      case "email":
        return "email-address";
      case "number":
        return "numeric";
      case "tel":
      case "phone":
        return "phone-pad";
      case "date":
        return "default";
      case "time":
        return "default";
      default:
        return "default";
    }
  };
  return /*#__PURE__*/React.createElement(_reactHookForm.Controller, {
    control: control,
    defaultValue: "",
    rules: {
      validate: v => (0, _helpers.isInputValid)(v, validation)
    },
    render: _ref => {
      let {
        field: {
          onChange,
          onBlur,
          value
        },
        formState: {
          errors
        }
      } = _ref;
      return /*#__PURE__*/React.createElement(_TextInput.default, _extends({
        onChangeText: onChange,
        onBlur: onBlur,
        value: value,
        isInvalid: !!errors[props.name],
        keyboardType: getKeyboardTypeFromType(props.type)
      }, rest));
    },
    name: props.name
  });
};

// TODO : Validation for select, checkbox, switch
const ControlledSelect = props => {
  const {
    ...rest
  } = props;
  const {
    control
  } = (0, _reactHookForm.useFormContext)();
  return /*#__PURE__*/React.createElement(_reactHookForm.Controller, {
    control: control,
    render: _ref2 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        },
        formState: {
          errors
        }
      } = _ref2;
      return /*#__PURE__*/React.createElement(_Select.default, _extends({
        style: {
          marginTop: 8
        },
        onSelect: onChange
      }, rest));
    },
    name: props.name
  });
};

//TODO: Move styling to RadioButtonGroup - make label clickable too
const ControlledRadio = props => {
  const {
    theme
  } = props;
  const {
    control
  } = (0, _reactHookForm.useFormContext)();
  return /*#__PURE__*/React.createElement(_reactHookForm.Controller, {
    control: control,
    defaultValue: props.defaultValue,
    render: _ref3 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        },
        formState: {
          errors
        }
      } = _ref3;
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        style: {
          width: "100%",
          backgroundColor: theme.colors.bg.subtle,
          paddingVertical: 16,
          paddingHorizontal: 8,
          borderRadius: 12,
          marginVertical: 8,
          flexDirection: "column"
        }
      }, props.label && /*#__PURE__*/React.createElement(_Text.default, {
        variant: "Caption",
        style: {
          fontWeight: "bold",
          paddingLeft: 8
        }
      }, props.label), props.desc && /*#__PURE__*/React.createElement(_Text.default, {
        style: {
          marginBottom: 8,
          paddingLeft: 8
        },
        variant: "XSmall"
      }, props.desc), /*#__PURE__*/React.createElement(_Radio.default.Group, {
        onValueChange: onChange,
        value: value
      }, props.options.map((option, i) => /*#__PURE__*/React.createElement(_reactNative.View, {
        key: i,
        style: {
          flexDirection: "row",
          alignItems: "center"
        }
      }, /*#__PURE__*/React.createElement(_Radio.default, {
        value: option.value
      }), /*#__PURE__*/React.createElement(_Text.default, {
        variant: "Input/Label",
        style: {
          textTransform: "uppercase",
          fontWeight: "bold",
          color: theme.colors.fg.subtle
        }
      }, option.label)))));
    },
    name: props.name
  });
};
const ControlledSwitch = props => {
  const {
    control
  } = (0, _reactHookForm.useFormContext)();
  return /*#__PURE__*/React.createElement(_reactHookForm.Controller, {
    control: control,
    defaultValue: false,
    render: _ref4 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        }
      } = _ref4;
      return (
        /*#__PURE__*/
        // TODO: move to Labeled Switch component
        React.createElement(_reactNative.View, {
          style: {
            width: "100%",
            marginVertical: 8
          }
        }, /*#__PURE__*/React.createElement(_reactNative.View, {
          style: {
            flexDirection: "row",
            justifyContent: "space-between",
            backgroundColor: props.theme.colors.bg.subtle,
            width: "100%",
            borderRadius: 12,
            height: 80,
            alignItems: "center",
            paddingHorizontal: 16
          }
        }, /*#__PURE__*/React.createElement(_Text.default, {
          style: {
            fontWeight: "bold"
          },
          variant: "Caption"
        }, props.label), /*#__PURE__*/React.createElement(_Switch.default, _extends({
          value: value,
          onValueChange: () => {
            onChange(!value);
          }
        }, props))), props.desc && /*#__PURE__*/React.createElement(_Text.default, {
          style: {
            marginLeft: 16,
            marginTop: 4,
            color: props.theme.colors.fg.subtle
          },
          variant: "XSmall"
        }, props.desc))
      );
    },
    name: props.name
  });
};
const ControlledCheckbox = props => {
  const {
    control
  } = (0, _reactHookForm.useFormContext)();
  return /*#__PURE__*/React.createElement(_reactHookForm.Controller, {
    control: control,
    defaultValue: props.defaultValue ?? false,
    render: _ref5 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        }
      } = _ref5;
      return (
        /*#__PURE__*/
        // TODO: move to Labeled Checkbox component
        React.createElement(_reactNative.TouchableWithoutFeedback, {
          style: {
            width: "100%",
            marginVertical: 8
          },
          onPress: () => onChange(!value)
        }, /*#__PURE__*/React.createElement(_reactNative.View, {
          style: {
            flexDirection: "column",
            justifyContent: "space-between",
            backgroundColor: props.theme.colors.bg.subtle,
            width: "100%",
            borderRadius: 12,
            paddingVertical: 16,
            paddingHorizontal: 16
          }
        }, /*#__PURE__*/React.createElement(_Text.default, {
          style: {
            fontWeight: "bold",
            marginBottom: 4
          },
          variant: "Caption"
        }, props.label), /*#__PURE__*/React.createElement(_reactNative.View, {
          style: {
            flexDirection: "row",
            alignItems: "center"
          }
        }, /*#__PURE__*/React.createElement(_Checkbox.default, {
          onPress: () => onChange(!value),
          status: value ? "checked" : "unchecked"
        }), typeof props.desc === "string" ? /*#__PURE__*/React.createElement(_Text.default, {
          style: {
            marginLeft: 4,
            color: props.theme.colors.fg.subtle
          },
          variant: "XSmall"
        }, props.desc) : props.desc && props.desc)))
      );
    },
    name: props.name
  });
};
const Form = _ref6 => {
  let {
    containerStyle,
    useFormMethods,
    theme,
    inputs,
    ...rest
  } = _ref6;
  const internalMethods = (0, _reactHookForm.useForm)({
    mode: "all"
  });
  const methods = useFormMethods ?? internalMethods;
  return /*#__PURE__*/React.createElement(_reactHookForm.FormProvider, methods, /*#__PURE__*/React.createElement(_reactNative.ScrollView, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: [styles.container, containerStyle]
  }, inputs.map((input, index) => {
    switch (input.type) {
      case "text":
      case "email":
      case "password":
      case "number":
      case "date":
      case "textarea":
      case "tel":
      case "phone":
      case "time":
        return /*#__PURE__*/React.createElement(_reactNative.View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledTextInput, _extends({
          theme: theme
        }, input)));
      case "switch":
        return /*#__PURE__*/React.createElement(_reactNative.View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledSwitch, _extends({
          theme: theme
        }, input)));
      case "select":
        return /*#__PURE__*/React.createElement(_reactNative.View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledSelect, _extends({
          theme: theme
        }, input)));
      case "radio":
      case "radio-group":
        return /*#__PURE__*/React.createElement(_reactNative.View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledRadio, _extends({
          theme: theme
        }, input)));
      case "checkbox":
        return /*#__PURE__*/React.createElement(_reactNative.View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledCheckbox, _extends({
          theme: theme
        }, input)));
      case "submit":
        const {
          type,
          onSubmit,
          onError,
          onPress,
          ...rest
        } = input;
        return /*#__PURE__*/React.createElement(_reactNative.View, {
          key: index,
          style: [{
            width: input.width ?? "100%",
            marginVertical: 8
          }]
        }, /*#__PURE__*/React.createElement(_Button.default, _extends({
          onPress: e => {
            methods.handleSubmit(onSubmit, onError)(e);
            typeof onPress === "function" && onPress(e);
          },
          key: index
        }, rest), rest.label ?? rest.children ?? "Submit"));
    }
  }))));
};
const styles = _reactNative.StyleSheet.create({
  container: {
    flexDirection: "row",
    flexWrap: "wrap",
    gap: 8
  }
});
var _default = (0, _theming.withTheme)(Form);
exports.default = _default;
//# sourceMappingURL=Form.js.map