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, { useEffect, useRef } from "react";
import { bool, string, shape, oneOfType, node } from "prop-types";
import styled, { withTheme } from "styled-components";
import { hexToRgb } from "@gympass/yoga-common";
import { Check, Rectangle } from "@gympass/yoga-icons";
import { useId } from "../../hooks";
import { HiddenInput } from "../../shared";
const CheckboxWrapper = styled.div`
  display: inline-block;

  * {
    box-sizing: border-box;
  }

  ${({ disabled }) => disabled ? `cursor: not-allowed` : ""}
`;
const CheckMark = styled.div.attrs(({ checked, disabled }) => ({
  role: "checkbox",
  "aria-checked": checked,
  "aria-disabled": disabled || void 0,
  "data-testid": "checkbox-checkMark"
}))`
  position: relative;

  border-style: solid;

  flex-shrink: 0;

  ${({
  checked,
  disabled,
  inverted,
  error,
  indeterminate,
  theme: {
    yoga: {
      colors: { primary, feedback, white },
      components: { checkbox }
    }
  }
}) => {
  let borderColor = checkbox.border.color;
  let bgColor = "transparent";
  let checkColor = checkbox.checked.icon.color;
  if (error) {
    [, borderColor] = feedback.attention;
    if (checked || indeterminate) {
      [, bgColor] = feedback.attention;
    }
  } else if (disabled) {
    borderColor = checkbox.disabled.border.color;
    if (checked || indeterminate) {
      bgColor = checkbox.disabled.backgroundColor;
      borderColor = checkbox.disabled.border.color;
    }
  } else if (checked || indeterminate) {
    borderColor = primary;
    bgColor = primary;
    checkColor = checkbox.checked.icon.color;
    if (inverted) {
      bgColor = white;
      borderColor = white;
      checkColor = primary;
    }
  } else if (inverted) {
    borderColor = white;
    checkColor = primary;
  }
  return `
    width: ${checkbox.size}px;
    height: ${checkbox.size}px;

    margin-right: ${checkbox.margin.right}px;

    background-color: ${bgColor};

    border-radius: ${checkbox.border.radius}px;
    border-width: ${checkbox.border.width}px;
    border-color: ${borderColor};

    svg {
      position: absolute;
      top: 50%;
      left: 50%;

      fill: ${checkColor};

      transform: translate(-50%, -50%);
    }`;
}}
`;
const Label = styled.label`
  display: flex;
  flex-direction: row;
  align-items: center;

  cursor: pointer;

  ${({
  disabled,
  theme: {
    yoga: {
      components: { checkbox }
    }
  }
}) => {
  let { color } = checkbox.label.font;
  if (disabled && checkbox.disabled.label) {
    color = checkbox.disabled.label.font.color;
  }
  return `
      font-size: ${checkbox.label.font.size}px;
      color: ${color};
    `;
}}
`;
const Shadow = styled.span`
  position: absolute;
  display: none;

  ${({
  theme: {
    yoga: {
      components: { checkbox }
    }
  }
}) => `
    width: ${checkbox.size}px;
    height: ${checkbox.size}px;
    border-radius: ${checkbox.hover.border.radius}px;
  `}
`;
const CheckboxStyled = styled.div`
  position: relative;
  display: flex;
  align-items: center;

  ${({
  checked,
  inverted,
  disabled,
  indeterminate,
  theme: {
    yoga: {
      colors: { primary, elements, white },
      components: { checkbox }
    }
  }
}) => {
  const shadowSize = Math.round(checkbox.size * 0.33);
  const shadowColor = () => {
    if (inverted) {
      return white;
    }
    return checked || indeterminate ? primary : elements.backgroundAndDisabled;
  };
  return `
      ${disabled ? `pointer-events: none` : ""}

      ${Label}:active,
      &:focus-within,
      &:hover  {
        ${Shadow} {
          display: block;
        }
      }

      &:hover {
        ${Shadow} {
          background-color: ${hexToRgb(shadowColor(), 0.25)};

          box-shadow: 0 0 0 ${shadowSize}px
            ${hexToRgb(shadowColor(), 0.25)};
        }
      }

      &:focus-within {
        ${Shadow} {
          background-color: ${hexToRgb(shadowColor(), 0.5)};

          box-shadow: 0 0 0 ${shadowSize}px
            ${hexToRgb(shadowColor(), 0.5)};
        }
      }

      ${Label}:active {
        ${Shadow} {
          background-color: ${hexToRgb(shadowColor(), 0.75)};

          box-shadow: 0 0 0 ${shadowSize}px
            ${hexToRgb(shadowColor(), 0.75)};
        }
      }
    `;
}}
`;
const HelperWrapper = styled.div`
  width: 100%;

  ${({
  theme: {
    yoga: {
      components: { checkbox }
    }
  }
}) => `
    margin-top: ${checkbox.helper.margin.top}px;
  `}
`;
const Helper = styled.span`
  ${({
  error,
  theme: {
    yoga: {
      colors: { feedback },
      components: { checkbox }
    }
  }
}) => `
    color: ${error ? feedback.attention[1] : checkbox.helper.font.color};

    font-size: ${checkbox.helper.font.size}px;
  `}
`;
const Checkbox = (_a) => {
  var _b = _a, {
    value,
    label,
    helper,
    disabled = false,
    checked = false,
    error,
    style,
    className,
    inverted = false,
    indeterminate = false,
    ariaLabel,
    theme: {
      yoga: {
        components: { checkbox }
      }
    }
  } = _b, rest = __objRest(_b, [
    "value",
    "label",
    "helper",
    "disabled",
    "checked",
    "error",
    "style",
    "className",
    "inverted",
    "indeterminate",
    "ariaLabel",
    "theme"
  ]);
  const inputRef = useRef(null);
  const id = useId();
  const checkboxLabelId = `checkbox-label-${id}`;
  const _a2 = rest, { onChange, onClick } = _a2, restWithoutEvents = __objRest(_a2, ["onChange", "onClick"]);
  useEffect(() => {
    if (inputRef.current) {
      inputRef.current.indeterminate = indeterminate;
    }
  });
  return /* @__PURE__ */ jsxs(
    CheckboxWrapper,
    __spreadProps(__spreadValues({
      style,
      className,
      disabled
    }, restWithoutEvents), {
      children: [
        /* @__PURE__ */ jsx(
          CheckboxStyled,
          {
            checked,
            indeterminate,
            inverted,
            disabled,
            children: /* @__PURE__ */ jsxs(Label, { id: checkboxLabelId, disabled, children: [
              /* @__PURE__ */ jsx(Shadow, {}),
              /* @__PURE__ */ jsxs(
                CheckMark,
                __spreadProps(__spreadValues({
                  "aria-labelledby": ariaLabel ? void 0 : checkboxLabelId,
                  "aria-label": ariaLabel
                }, {
                  disabled,
                  checked,
                  error,
                  inverted,
                  indeterminate
                }), {
                  children: [
                    checked && !indeterminate && /* @__PURE__ */ jsx(Check, { width: checkbox.size, height: checkbox.size }),
                    indeterminate && /* @__PURE__ */ jsx(Rectangle, { width: checkbox.size, height: checkbox.size })
                  ]
                })
              ),
              /* @__PURE__ */ jsx(
                HiddenInput,
                __spreadProps(__spreadValues(__spreadValues({
                  type: "checkbox",
                  ref: inputRef,
                  checked,
                  disabled
                }, value ? { value } : {}), restWithoutEvents), {
                  onChange,
                  onClick,
                  "aria-hidden": true
                })
              ),
              label
            ] })
          }
        ),
        (helper || error) && /* @__PURE__ */ jsx(HelperWrapper, { children: /* @__PURE__ */ jsx(Helper, { error, children: error || helper }) })
      ]
    })
  );
};
Checkbox.propTypes = {
  label: oneOfType([string, node]),
  /** short helper text under checkbox */
  helper: string,
  value: string,
  checked: bool,
  disabled: bool,
  inverted: bool,
  error: string,
  /** if true, the component appears indeterminate */
  indeterminate: bool,
  /** set a style to the checkbox container */
  style: shape({}),
  className: string,
  ariaLabel: string
};
Checkbox.displayName = "Checkbox";
var Checkbox_default = withTheme(Checkbox);
export {
  Checkbox_default as default
};
