"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __getProtoOf = Object.getPrototypeOf;
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;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Checkbox_exports = {};
__export(Checkbox_exports, {
  default: () => Checkbox_default
});
module.exports = __toCommonJS(Checkbox_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_prop_types = require("prop-types");
var import_styled_components = __toESM(require("styled-components"));
var import_yoga_common = require("@gympass/yoga-common");
var import_yoga_icons = require("@gympass/yoga-icons");
var import_hooks = require("../../hooks");
var import_shared = require("../../shared");
const CheckboxWrapper = import_styled_components.default.div`
  display: inline-block;

  * {
    box-sizing: border-box;
  }

  ${({ disabled }) => disabled ? `cursor: not-allowed` : ""}
`;
const CheckMark = import_styled_components.default.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, elements, white },
      components: { checkbox }
    }
  }
}) => {
  let borderColor = elements.selectionAndIcons;
  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 = elements.lineAndBorders;
    }
  } 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 = import_styled_components.default.label`
  display: flex;
  flex-direction: row;
  align-items: center;

  cursor: pointer;

  ${({
  theme: {
    yoga: {
      components: { checkbox }
    }
  }
}) => `
    font-size: ${checkbox.label.font.size}px;
    color: ${checkbox.label.font.color};
  `}
`;
const Shadow = import_styled_components.default.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 = import_styled_components.default.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: ${(0, import_yoga_common.hexToRgb)(shadowColor(), 0.25)};

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

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

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

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

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

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