import {
  logger,
  useAddressContext,
  useSteps,
  useUserContext,
} from "@deuna/checkout-core";
import { Flex } from "@deuna/components/layout";
import { Button } from "@deuna/components/button";

interface ContinueButtonProps {
  withTermsAndConditions?: boolean;
  withBillingAddress?: boolean;
}

const ContinueButton = (props: ContinueButtonProps) => {
  const { state: userState } = useUserContext();
  const { getAddressesByEmail, maskedAddresses } = useAddressContext();
  const { actions } = useSteps();

  // const isValid = EmailPattern.getValidator().isValid;

  const handleOnContinue = async () => {
    const data = await getAddressesByEmail(userState.userInfo.email);

    if (data.error || data.data?.data.length === 0) {
      logger.emit({
        type: "error",
        name: "",
        fileLocation: "ContinueButton",
        functionName: "fetchAddressesByEmail",
        message:
          "No eres un cliente registrado. Para utilizar esta función, debes hacer al menos una compra. ",
      });
      return;
    }

    if (maskedAddresses) {
      actions.goToNext();
    }
  };

  return (
    <Flex>
      <Button
        style={{ width: "100%", marginTop: "0px" }}
        onClick={handleOnContinue}
        // disabled={!isValid}
      >
        Guardar
      </Button>
    </Flex>
  );
};

export default ContinueButton;
