"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 Dropdown_exports = {};
__export(Dropdown_exports, {
  default: () => Dropdown_default
});
module.exports = __toCommonJS(Dropdown_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_prop_types = require("prop-types");
var import_react_native = require("react-native");
var import_yoga_icons = require("@gympass/yoga-icons");
var import_Options = __toESM(require("./Options"));
var import_Backdrop = __toESM(require("./Backdrop"));
var import_Text = __toESM(require("../../Text"));
var import_Helper = __toESM(require("../../Input/native/Helper"));
const Selector = import_styled_components.default.View`
  ${({
  disabled,
  selected,
  error,
  full,
  theme: {
    yoga: {
      colors,
      components: { dropdown }
    }
  }
}) => `
    position: relative;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;

    width: ${full ? "100%" : `${dropdown.width}px`};
    padding: ${dropdown.selector.padding.top}px
      ${dropdown.selector.padding.right}px
      ${dropdown.selector.padding.bottom}px
      ${dropdown.selector.padding.left}px;

    background-color: ${dropdown.selector.background};
    border-radius: ${dropdown.selector.border.radius}px;
    border-width: ${dropdown.selector.border.width}px;
    border-style: solid;
    border-color: ${error ? colors.feedback.attention[1] : dropdown.selector.border.color};

    ${disabled ? `border-color: ${dropdown.disabled.selector.border.color};` : ""};
    ${selected ? `border-color: ${dropdown.selected.selector.border.color};` : ""};
  `}
`;
const Label = (0, import_styled_components.default)(import_Text.default.Body2)`
  ${({
  disabled,
  selected,
  theme: {
    yoga: {
      components: { dropdown }
    }
  }
}) => `
    color: ${dropdown.input.font.color};
    ${disabled ? `color: ${dropdown.disabled.input.font.color};` : ""}
    ${selected ? `color: ${dropdown.selected.input.font.color};` : ""}
  `}
`;
const getSelectedOption = (options) => options.find((item) => item.selected === true);
const Dropdown = import_react.default.forwardRef(
  (_a, ref) => {
    var _b = _a, {
      error = void 0,
      label = "",
      disabled = false,
      full = false,
      options,
      cancelActionLabel = "Cancel",
      confirmActionLabel = "Confirm",
      onChange = () => {
      },
      theme: {
        yoga: {
          components: { dropdown }
        }
      }
    } = _b, rest = __objRest(_b, [
      "error",
      "label",
      "disabled",
      "full",
      "options",
      "cancelActionLabel",
      "confirmActionLabel",
      "onChange",
      "theme"
    ]);
    const [selected, toggleIsSelected] = (0, import_react.useState)(getSelectedOption(options));
    const [isOpen, toggleIsOpen] = (0, import_react.useState)(false);
    const iconColor = () => {
      if (disabled)
        return dropdown.disabled.arrow.fill;
      if (selected)
        return dropdown.selected.arrow.fill;
      return dropdown.arrow.fill;
    };
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_react_native.TouchableWithoutFeedback,
        {
          accessibilityRole: "button",
          onPress: () => !disabled && toggleIsOpen(true),
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native.View, { children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
              Selector,
              __spreadProps(__spreadValues({
                full,
                disabled,
                selected,
                error
              }, rest), {
                ref,
                children: [
                  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Label, { disabled, selected, children: selected && selected.label || label }),
                  /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_yoga_icons.ChevronDown, { width: 20, height: 20, fill: iconColor() })
                ]
              })
            ),
            error && !selected && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_Helper.default, { error })
          ] })
        }
      ),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_Backdrop.default,
        {
          visible: isOpen,
          title: label,
          onClose: () => toggleIsOpen(false),
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_Options.default,
            {
              options,
              selectedOption: selected,
              cancelActionLabel,
              confirmActionLabel,
              onClose: () => toggleIsOpen(false),
              onSelect: (item) => {
                toggleIsSelected(item);
                onChange(item);
                toggleIsOpen(false);
              }
            }
          )
        }
      )
    ] });
  }
);
Dropdown.propTypes = {
  label: import_prop_types.string,
  disabled: import_prop_types.bool,
  error: import_prop_types.string,
  full: import_prop_types.bool,
  /** { label (string), value (string or number), selected: (boolean) } */
  options: (0, import_prop_types.arrayOf)(
    (0, import_prop_types.shape)({
      label: import_prop_types.string,
      value: (0, import_prop_types.oneOfType)([import_prop_types.string, import_prop_types.number]),
      selected: import_prop_types.bool
    })
  ).isRequired,
  /** Cancel label that will be shown in iOS option chooser */
  cancelActionLabel: import_prop_types.string,
  /** Confirm label that will be shown in iOS option chooser */
  confirmActionLabel: import_prop_types.string,
  onChange: import_prop_types.func
};
var Dropdown_default = (0, import_styled_components.withTheme)(Dropdown);
