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); }
import * as React from "react";
import { useForm, FormProvider, useFormContext, Controller } from "react-hook-form";
import { StyleSheet, View, ScrollView, TouchableWithoutFeedback } from "react-native";
import { withTheme } from "../../core/theming";
import Button from "../Button";
import Checkbox from "../Checkbox/Checkbox";
import { isInputValid } from "../Input/helpers";
import TextInput from "../Input/TextInput";
import RadioButton from "../Radio";
import Select from "../Select";
import Switch from "../Switch/Switch";
import Text from "../Typography/Text";
const ControlledTextInput = props => {
  const {
    validation,
    ...rest
  } = props;
  const {
    control
  } = 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(Controller, {
    control: control,
    defaultValue: "",
    rules: {
      validate: v => isInputValid(v, validation)
    },
    render: _ref => {
      let {
        field: {
          onChange,
          onBlur,
          value
        },
        formState: {
          errors
        }
      } = _ref;
      return /*#__PURE__*/React.createElement(TextInput, _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
  } = useFormContext();
  return /*#__PURE__*/React.createElement(Controller, {
    control: control,
    render: _ref2 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        },
        formState: {
          errors
        }
      } = _ref2;
      return /*#__PURE__*/React.createElement(Select, _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
  } = useFormContext();
  return /*#__PURE__*/React.createElement(Controller, {
    control: control,
    defaultValue: props.defaultValue,
    render: _ref3 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        },
        formState: {
          errors
        }
      } = _ref3;
      return /*#__PURE__*/React.createElement(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, {
        variant: "Caption",
        style: {
          fontWeight: "bold",
          paddingLeft: 8
        }
      }, props.label), props.desc && /*#__PURE__*/React.createElement(Text, {
        style: {
          marginBottom: 8,
          paddingLeft: 8
        },
        variant: "XSmall"
      }, props.desc), /*#__PURE__*/React.createElement(RadioButton.Group, {
        onValueChange: onChange,
        value: value
      }, props.options.map((option, i) => /*#__PURE__*/React.createElement(View, {
        key: i,
        style: {
          flexDirection: "row",
          alignItems: "center"
        }
      }, /*#__PURE__*/React.createElement(RadioButton, {
        value: option.value
      }), /*#__PURE__*/React.createElement(Text, {
        variant: "Input/Label",
        style: {
          textTransform: "uppercase",
          fontWeight: "bold",
          color: theme.colors.fg.subtle
        }
      }, option.label)))));
    },
    name: props.name
  });
};
const ControlledSwitch = props => {
  const {
    control
  } = useFormContext();
  return /*#__PURE__*/React.createElement(Controller, {
    control: control,
    defaultValue: false,
    render: _ref4 => {
      let {
        field: {
          onChange,
          onBlur,
          value
        }
      } = _ref4;
      return (
        /*#__PURE__*/
        // TODO: move to Labeled Switch component
        React.createElement(View, {
          style: {
            width: "100%",
            marginVertical: 8
          }
        }, /*#__PURE__*/React.createElement(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, {
          style: {
            fontWeight: "bold"
          },
          variant: "Caption"
        }, props.label), /*#__PURE__*/React.createElement(Switch, _extends({
          value: value,
          onValueChange: () => {
            onChange(!value);
          }
        }, props))), props.desc && /*#__PURE__*/React.createElement(Text, {
          style: {
            marginLeft: 16,
            marginTop: 4,
            color: props.theme.colors.fg.subtle
          },
          variant: "XSmall"
        }, props.desc))
      );
    },
    name: props.name
  });
};
const ControlledCheckbox = props => {
  const {
    control
  } = useFormContext();
  return /*#__PURE__*/React.createElement(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(TouchableWithoutFeedback, {
          style: {
            width: "100%",
            marginVertical: 8
          },
          onPress: () => onChange(!value)
        }, /*#__PURE__*/React.createElement(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, {
          style: {
            fontWeight: "bold",
            marginBottom: 4
          },
          variant: "Caption"
        }, props.label), /*#__PURE__*/React.createElement(View, {
          style: {
            flexDirection: "row",
            alignItems: "center"
          }
        }, /*#__PURE__*/React.createElement(Checkbox, {
          onPress: () => onChange(!value),
          status: value ? "checked" : "unchecked"
        }), typeof props.desc === "string" ? /*#__PURE__*/React.createElement(Text, {
          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 = useForm({
    mode: "all"
  });
  const methods = useFormMethods ?? internalMethods;
  return /*#__PURE__*/React.createElement(FormProvider, methods, /*#__PURE__*/React.createElement(ScrollView, null, /*#__PURE__*/React.createElement(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(View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledTextInput, _extends({
          theme: theme
        }, input)));
      case "switch":
        return /*#__PURE__*/React.createElement(View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledSwitch, _extends({
          theme: theme
        }, input)));
      case "select":
        return /*#__PURE__*/React.createElement(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(View, {
          key: index,
          style: [{
            width: input.width ?? "100%"
          }]
        }, /*#__PURE__*/React.createElement(ControlledRadio, _extends({
          theme: theme
        }, input)));
      case "checkbox":
        return /*#__PURE__*/React.createElement(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(View, {
          key: index,
          style: [{
            width: input.width ?? "100%",
            marginVertical: 8
          }]
        }, /*#__PURE__*/React.createElement(Button, _extends({
          onPress: e => {
            methods.handleSubmit(onSubmit, onError)(e);
            typeof onPress === "function" && onPress(e);
          },
          key: index
        }, rest), rest.label ?? rest.children ?? "Submit"));
    }
  }))));
};
const styles = StyleSheet.create({
  container: {
    flexDirection: "row",
    flexWrap: "wrap",
    gap: 8
  }
});
export default withTheme(Form);
//# sourceMappingURL=Form.js.map