"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 Switch_exports = {};
__export(Switch_exports, {
  default: () => Switch_default
});
module.exports = __toCommonJS(Switch_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_react_native = require("react-native");
var import_withTouchable = __toESM(require("../../Button/native/withTouchable"));
var import_Box = __toESM(require("../../Box"));
const SwitchTrack = import_styled_components.default.View`
  ${({
  theme: {
    yoga: {
      components: { checkboxswitch }
    }
  },
  checked,
  disabled
}) => `
    display: flex;
    justify-content: center;
    width: ${checkboxswitch.track.width}px;
    height: ${checkboxswitch.track.height}px;
    borderRadius: ${checkboxswitch.track.radii}px;
    background-color: ${checked ? checkboxswitch.track.checked.backgroundColor : checkboxswitch.track.backgroundColor};

    ${disabled ? `
        background-color: ${checkboxswitch.track.disabled.backgroundColor};` : ""}
  `};
`;
const ThumbShadow = import_styled_components.default.View(
  ({
    theme: {
      yoga: {
        colors,
        components: { checkboxswitch }
      }
    },
    checked
  }) => {
    const width = checkboxswitch.thumb.width * checkboxswitch.thumb.shadowScale;
    const height = checkboxswitch.thumb.height * checkboxswitch.thumb.shadowScale;
    return `
      width: ${width}px;
      height: ${height}px;
      position: absolute;
      background-color: ${checked ? (0, import_yoga_common.hexToRgb)(colors.primary, 0.75) : (0, import_yoga_common.hexToRgb)(colors.elements.backgroundAndDisabled, 0.75)};
      border-radius: ${checkboxswitch.thumb.radii}px;
    `;
  }
);
const SwitchThumb = (0, import_styled_components.default)(import_Box.default).attrs(
  ({
    theme: {
      yoga: {
        components: { checkboxswitch }
      }
    },
    disabled
  }) => ({
    width: checkboxswitch.thumb.width,
    height: checkboxswitch.thumb.height,
    borderRadius: checkboxswitch.thumb.radii,
    bgColor: disabled ? checkboxswitch.thumb.disabled.backgroundColor : checkboxswitch.thumb.backgroundColor,
    elevation: checkboxswitch.thumb.shadow
  })
)``;
const CheckboxSwitch = ({
  checked = false,
  pressed = false,
  disabled = false,
  theme: {
    yoga: {
      components: { checkboxswitch }
    }
  }
}) => {
  const [thumbPosition] = (0, import_react.useState)(new import_react_native.Animated.Value(Number(checked)));
  const thumbTo = checkboxswitch.track.width - checkboxswitch.thumb.width - checkboxswitch.thumb.left;
  const thumbFrom = checkboxswitch.thumb.left;
  const shadowDiameter = checkboxswitch.thumb.width * checkboxswitch.thumb.shadowScale;
  const shadowAndThumbDiff = shadowDiameter - checkboxswitch.thumb.width;
  const halfDiff = shadowAndThumbDiff / 2;
  const thumbShadowFrom = (halfDiff - checkboxswitch.thumb.left) * -1;
  const thumbShadowTo = thumbTo - halfDiff;
  (0, import_react.useEffect)(() => {
    const toggle = (isChecked, position) => {
      const animValue = {
        fromValue: isChecked ? 0 : 1,
        toValue: isChecked ? 1 : 0,
        duration: 100,
        useNativeDriver: false
      };
      import_react_native.Animated.timing(position, animValue).start();
    };
    toggle(!checked, thumbPosition);
  }, [checked]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    SwitchTrack,
    {
      checked,
      disabled,
      as: import_react_native.Animated.View,
      style: !disabled && {
        backgroundColor: thumbPosition.interpolate({
          inputRange: [0, 1],
          outputRange: [
            checkboxswitch.track.checked.backgroundColor,
            checkboxswitch.track.backgroundColor
          ]
        })
      },
      children: [
        pressed && !disabled && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          ThumbShadow,
          {
            checked,
            as: import_react_native.Animated.View,
            style: {
              transform: [
                {
                  translateX: thumbPosition.interpolate({
                    inputRange: [0, 1],
                    outputRange: [thumbShadowTo, thumbShadowFrom]
                  })
                }
              ]
            }
          }
        ),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          SwitchThumb,
          {
            checked,
            disabled,
            as: import_react_native.Animated.View,
            style: {
              transform: [
                {
                  translateX: thumbPosition.interpolate({
                    inputRange: [0, 1],
                    outputRange: [thumbTo, thumbFrom]
                  })
                }
              ]
            }
          }
        )
      ]
    }
  );
};
CheckboxSwitch.propTypes = {
  checked: import_prop_types.bool,
  disabled: import_prop_types.bool,
  pressed: import_prop_types.bool
};
CheckboxSwitch.displayName = "Checkbox.Switch";
var Switch_default = (0, import_withTouchable.default)((0, import_styled_components.withTheme)(CheckboxSwitch));
