"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const errorMessage = require("@hookform/error-message");
const reactHookForm = require("react-hook-form");
;/* empty css                                                                                                                                                                                           */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                                    */
;/* empty css                                                                                                                                                                                               */
;/* empty css                                                                                                                                                                                               */
require("clsx");
const styles = require("../../../utils/styles.cjs");
const index = require("../../PhoneInput/index.cjs");
const Select = require("../Select.cjs");
const rawCountries = require("./rawCountries.cjs");
const Box = require("../../../node_modules/.pnpm/@mantine_core@7.17.8_patch_hash_3qirwkgu3wxhvd6hnr2gb24yzq_@mantine_hooks@7.17.8_react@18.3.1_e5yvlgwhmeykc75dkphi7jou6q/node_modules/@mantine/core/esm/core/Box/Box.cjs");
const createStyles = require("../../../node_modules/.pnpm/@mantine_emotion@7.17.8_patch_hash_op2x5ib3e7mgdsoy22n5xoaf4q_@emotion_cache@11.14.0_@emotion_54ygowx4natmcqkxvanlwrr74u/node_modules/@mantine/emotion/esm/create-styles.cjs");
const getThemeColor = require("../../../node_modules/.pnpm/@mantine_core@7.17.8_patch_hash_3qirwkgu3wxhvd6hnr2gb24yzq_@mantine_hooks@7.17.8_react@18.3.1_e5yvlgwhmeykc75dkphi7jou6q/node_modules/@mantine/core/esm/core/MantineProvider/color-functions/get-theme-color/get-theme-color.cjs");
const FormPhoneInput = ({
  name,
  rules,
  onChange: onInputChange,
  onSelect,
  label,
  rootProps,
  ...rest
}) => {
  const { control, formState, getFieldState } = reactHookForm.useFormContext();
  const { error } = getFieldState(name, formState);
  return /* @__PURE__ */ jsxRuntime.jsx(
    reactHookForm.Controller,
    {
      name,
      control,
      rules,
      render: ({ field: { value, onChange } }) => {
        const handleChange = (value2, data, event, formattedValue) => {
          onChange(formattedValue);
          onSelect == null ? void 0 : onSelect(formattedValue, data);
          onInputChange == null ? void 0 : onInputChange(formattedValue, data, event, formattedValue);
        };
        return /* @__PURE__ */ jsxRuntime.jsx(
          index.PhoneInput,
          {
            error: error ? /* @__PURE__ */ jsxRuntime.jsx(errorMessage.ErrorMessage, { errors: formState.errors, name }) : void 0,
            value,
            label,
            onChange: handleChange,
            rootProps,
            ...rest
          }
        );
      }
    }
  );
};
const countryOptions = rawCountries.default.map((raw) => {
  return {
    // country code
    value: raw[2],
    // country name
    label: `${raw[0]}`
  };
});
const useStyles = createStyles.createStyles((theme) => {
  const errorColor = getThemeColor.getThemeColor("red", theme);
  return {
    phoneInputContainer: {
      borderTopLeftRadius: "0 !important",
      borderBottomLeftRadius: "0 !important",
      transform: "translateX(-1px)"
    },
    error: {
      color: errorColor,
      lineHeight: 1.2,
      fontSize: 12
    }
  };
});
const FormPhoneInputV2 = ({
  countryKey,
  phoneKey,
  selectProps = {},
  countryRules,
  rules,
  rootProps,
  ...rest
}) => {
  const { watch, formState, getFieldState } = reactHookForm.useFormContext();
  const { filterData: onFilter, ...restProps } = selectProps;
  const { error: countryError } = getFieldState(countryKey, formState);
  const { error: phoneError } = getFieldState(phoneKey, formState);
  const country = watch(countryKey, "");
  const { classes } = useStyles();
  return /* @__PURE__ */ jsxRuntime.jsxs(Box.Box, { ...rootProps, children: [
    /* @__PURE__ */ jsxRuntime.jsxs(Box.Box, { display: "flex", children: [
      /* @__PURE__ */ jsxRuntime.jsx(
        Select.FormSelect,
        {
          data: onFilter ? countryOptions.filter(onFilter) : countryOptions,
          ...restProps,
          name: countryKey,
          rules: countryRules,
          styles: styles.mergeStylesList([
            () => {
              return {
                wrapper: {
                  ":hover": {
                    zIndex: 1
                  },
                  ":focus-within": {
                    zIndex: 1
                  },
                  zIndex: countryError ? 1 : void 0
                },
                error: {
                  display: "none"
                },
                input: {
                  borderTopRightRadius: 0,
                  borderBottomRightRadius: 0
                }
              };
            },
            restProps.styles
          ])
        }
      ),
      /* @__PURE__ */ jsxRuntime.jsx(
        FormPhoneInput,
        {
          country,
          rootProps: { w: "100%", mb: 0 },
          rules,
          inputClass: classes.phoneInputContainer,
          styles: {
            error: {
              display: "none"
            }
          },
          showContryCodeAfterFocus: false,
          name: phoneKey,
          ...rest
        }
      )
    ] }),
    (countryError || phoneError) && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: classes.error, children: [
      /* @__PURE__ */ jsxRuntime.jsx(errorMessage.ErrorMessage, { errors: formState.errors, name: countryKey }),
      /* @__PURE__ */ jsxRuntime.jsx(errorMessage.ErrorMessage, { errors: formState.errors, name: phoneKey })
    ] })
  ] });
};
exports.FormPhoneInput = FormPhoneInput;
exports.FormPhoneInputV2 = FormPhoneInputV2;
