var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
import { jsx, jsxs } from "react/jsx-runtime";
import React, { useState } from "react";
import { bool, func, string, shape } from "prop-types";
import styled, { withTheme } from "styled-components";
import { TouchableWithoutFeedback, View } from "react-native";
import { hexToRgb } from "@gympass/yoga-common";
import { Check } from "@gympass/yoga-icons";
import Text from "../../Text";
const CheckboxWrapper = styled.View`
  flex-direction: row;
  align-items: center;
`;
const Label = styled(Text.Regular)(
  ({
    theme: {
      yoga: {
        components: { checkbox }
      }
    }
  }) => `
  padding-left: ${checkbox.label.padding.left}px;

  font-size: ${checkbox.label.font.size}px;

  color: ${checkbox.label.font.color};
`
);
const HelperWrapper = styled.View(
  ({
    theme: {
      yoga: {
        components: { checkbox }
      }
    }
  }) => `
  margin-top: ${checkbox.helper.margin.top}px;
`
);
const Helper = styled(Text.Regular)(
  ({
    error,
    theme: {
      yoga: {
        colors: { feedback },
        components: { checkbox }
      }
    }
  }) => `
  font-size: ${checkbox.helper.font.size}px;

  color: ${error ? feedback.attention[1] : checkbox.helper.font.color};
`
);
const CheckBackground = styled.View(
  ({
    checked,
    disabled,
    inverted,
    error,
    theme: {
      yoga: {
        colors: { primary, feedback, white },
        components: { checkbox }
      }
    }
  }) => {
    let borderColor = checkbox.border.color;
    let bgColor = "transparent";
    if (error) {
      [, borderColor] = feedback.attention;
      if (checked) {
        [, bgColor] = feedback.attention;
      }
    } else if (disabled) {
      borderColor = checkbox.disabled.border.color;
      if (checked) {
        bgColor = checkbox.disabled.backgroundColor;
      }
    } else if (checked) {
      borderColor = primary;
      bgColor = primary;
      if (inverted) {
        bgColor = white;
        borderColor = white;
      }
    } else if (inverted) {
      borderColor = white;
    }
    return `
      position: absolute;

      width: ${checkbox.size}px;
      height: ${checkbox.size}px;

      border-radius: ${checkbox.border.radius}px;
      border-width: ${checkbox.border.width}px;
      border-style: solid;

      background-color: ${bgColor};

      border-color: ${borderColor};
    `;
  }
);
const Shadow = styled.View(
  ({
    checked,
    inverted,
    theme: {
      yoga: {
        colors: { primary, elements, white },
        components: { checkbox }
      }
    }
  }) => {
    const size = checkbox.size * 1.67;
    const shadowColor = checked ? primary : elements.backgroundAndDisabled;
    return `
      width: ${size}px;
      height: ${size}px;

      background-color: ${hexToRgb(inverted ? white : shadowColor, 0.75)};
      border-radius: ${checkbox.hover.border.radius}px;
    `;
  }
);
const CheckArea = styled.View(
  ({
    theme: {
      yoga: {
        components: { checkbox }
      }
    }
  }) => `
  justify-content: center;
  align-items: center;

  width: ${checkbox.size}px;
  height: ${checkbox.size}px;
`
);
const Checkbox = (_a) => {
  var _b = _a, {
    label = void 0,
    helper = void 0,
    disabled = false,
    checked = false,
    error = void 0,
    style = {},
    onPressIn = () => {
    },
    onPressOut = () => {
    },
    inverted = false,
    theme: {
      yoga: {
        components: { checkbox },
        colors
      }
    }
  } = _b, rest = __objRest(_b, [
    "label",
    "helper",
    "disabled",
    "checked",
    "error",
    "style",
    "onPressIn",
    "onPressOut",
    "inverted",
    "theme"
  ]);
  const [pressed, setPressed] = useState(false);
  return /* @__PURE__ */ jsxs(View, { children: [
    /* @__PURE__ */ jsx(
      TouchableWithoutFeedback,
      __spreadProps(__spreadValues({}, rest), {
        disabled,
        onPressIn: (e) => {
          setPressed(true);
          onPressIn(e);
        },
        onPressOut: (e) => {
          setPressed(false);
          onPressOut(e);
        },
        children: /* @__PURE__ */ jsxs(CheckboxWrapper, { style, children: [
          /* @__PURE__ */ jsxs(CheckArea, { children: [
            pressed && !disabled && /* @__PURE__ */ jsx(Shadow, { pressed, checked, inverted }),
            /* @__PURE__ */ jsx(
              CheckBackground,
              __spreadValues({}, {
                disabled,
                checked,
                error,
                pressed,
                inverted
              })
            ),
            checked && /* @__PURE__ */ jsx(
              Check,
              {
                fill: inverted ? colors.primary : checkbox.checked.icon.color,
                style: { position: "absolute" },
                width: 24,
                height: 24
              }
            )
          ] }),
          label && /* @__PURE__ */ jsx(Label, { children: label })
        ] })
      })
    ),
    (helper || error) && /* @__PURE__ */ jsx(HelperWrapper, { children: /* @__PURE__ */ jsx(Helper, { error, children: error || helper }) })
  ] });
};
Checkbox.propTypes = {
  label: string,
  /** set a short helper text under checkbox */
  helper: string,
  checked: bool,
  disabled: bool,
  inverted: bool,
  error: string,
  style: shape({}),
  onPressIn: func,
  onPressOut: func
};
Checkbox.displayName = "Checkbox";
var Checkbox_default = withTheme(Checkbox);
export {
  Checkbox_default as default
};
