"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);

// src/hooks/index.tsx
var hooks_exports = {};
__export(hooks_exports, {
  DEFAULT_BREAKPOINTS: () => DEFAULT_BREAKPOINTS,
  useBreakpoint: () => useBreakpoint,
  useButton: () => useButton,
  useDialog: () => useDialog,
  useDialogPolyfill: () => useDialogPolyfill,
  useLabel: () => useLabel,
  useTextField: () => useTextField,
  useValidationClasses: () => useValidationClasses
});
module.exports = __toCommonJS(hooks_exports);

// src/hooks/useBreakpoint/useBreakpoint.tsx
var import_mobius_hooks = require("@simplybusiness/mobius-hooks");
var import_react = require("react");
var DEFAULT_BREAKPOINTS = [
  { size: "xs", value: 320 },
  { size: "sm", value: 480 },
  { size: "md", value: 670 },
  { size: "lg", value: 960 },
  { size: "xl", value: 1200 },
  { size: "xxl", value: 1320 }
];
var PASSIVE = { passive: true };
var getBreakpoint = (breakpoints, windowWidth) => {
  const closest = breakpoints.findIndex(
    (breakpoint) => breakpoint.value >= windowWidth + 1
  );
  const smallest = breakpoints[0];
  const largest = breakpoints[breakpoints.length - 1];
  const match = breakpoints[closest - 1];
  if (closest === 0) return smallest;
  if (closest === -1) return largest;
  return match;
};
var useBreakpoint = (customBreakpoints) => {
  const isClientSide = (0, import_mobius_hooks.useIsClient)();
  const breakpoints = customBreakpoints || DEFAULT_BREAKPOINTS;
  const defaultBreakpoint = breakpoints[0];
  const [windowWidth, setWindowWidth] = (0, import_react.useState)(
    typeof globalThis?.window !== "undefined" ? window.innerWidth : defaultBreakpoint.value
  );
  (0, import_mobius_hooks.useWindowEvent)("resize", () => setWindowWidth(window.innerWidth), PASSIVE);
  const debouncedWidth = (0, import_mobius_hooks.useDebouncedValue)(windowWidth, 200);
  const currentBreakpoint = (0, import_react.useMemo)(
    () => !isClientSide || breakpoints.length === 0 ? defaultBreakpoint : getBreakpoint(breakpoints, debouncedWidth),
    [isClientSide, breakpoints, defaultBreakpoint, debouncedWidth]
  );
  const up = (0, import_react.useCallback)(
    (size) => {
      const sizeIndex = breakpoints.findIndex((item) => item.size === size);
      if (sizeIndex === -1) {
        return false;
      }
      const currentIndex = breakpoints.findIndex(
        (item) => item.size === currentBreakpoint.size
      );
      return currentIndex >= sizeIndex;
    },
    [currentBreakpoint, breakpoints]
  );
  const down = (0, import_react.useCallback)(
    (size) => {
      const sizeIndex = breakpoints.findIndex((item) => item.size === size);
      if (sizeIndex === -1) {
        return true;
      }
      const currentIndex = breakpoints.findIndex(
        (item) => item.size === currentBreakpoint.size
      );
      return currentIndex <= sizeIndex;
    },
    [currentBreakpoint, breakpoints]
  );
  return {
    breakpoint: currentBreakpoint,
    up,
    down
  };
};

// src/hooks/useButton/useButton.tsx
var import_react2 = require("react");
function useButton({
  elementType = "button",
  type = "button",
  isDisabled = false,
  href,
  target,
  rel,
  role,
  name,
  value,
  onClick,
  onPress
}) {
  const realOnClick = (0, import_react2.useCallback)(
    (event) => {
      if (isDisabled) {
        return;
      }
      onClick?.(event);
      onPress?.(event);
    },
    [isDisabled, onClick, onPress]
  );
  function getRole() {
    if (role) {
      return role;
    }
    if (elementType === "a") {
      return void 0;
    }
    return "button";
  }
  const extraProps = elementType === "button" ? { type } : {
    role: getRole()
  };
  return {
    buttonProps: {
      href: elementType === "a" ? href : void 0,
      target: elementType === "a" ? target : void 0,
      rel: elementType === "a" ? rel : void 0,
      tabIndex: isDisabled ? -1 : 0,
      disabled: isDisabled || void 0,
      "aria-disabled": isDisabled || void 0,
      name: elementType === "input" || elementType === "button" ? name : void 0,
      value: elementType === "input" || elementType === "button" ? value : void 0,
      ...extraProps,
      onClick: realOnClick
    }
  };
}

// src/hooks/useDialog/useDialog.ts
var import_react4 = require("react");

// src/utils/polyfill-tests.ts
var supportsDialog = () => typeof HTMLDialogElement === "function";

// src/utils/spaceDelimitedList.ts
function spaceDelimitedList(list) {
  return list.filter(Boolean).join(" ") || void 0;
}

// src/hooks/useDialog/useDialog.ts
var import_mobius_hooks2 = require("@simplybusiness/mobius-hooks");

// src/hooks/useDialogPolyfill/useDialogPolyfill.ts
var import_react3 = require("react");
var useDialogPolyfill = () => {
  const [hasDialogSupport, setHasDialogSupport] = (0, import_react3.useState)(false);
  const hasAlreadyLoaded = (0, import_react3.useRef)(false);
  (0, import_react3.useEffect)(() => {
    setHasDialogSupport(supportsDialog());
  }, []);
  const polyfillDialog = async (ref) => {
    if (!hasAlreadyLoaded.current && !hasDialogSupport && typeof window !== "undefined" && ref.current !== null) {
      const { default: dialogPolyfill } = await import("dialog-polyfill");
      hasAlreadyLoaded.current = true;
      try {
        if (ref.current) {
          dialogPolyfill.registerDialog(ref.current);
        }
      } catch (error) {
        console.error("Failed to load dialog-polyfill", error);
      }
    }
  };
  return {
    polyfillDialog
  };
};

// src/hooks/useDialog/useDialog.ts
var TRANSITION_CLASS_NAME = "--transition";
var FALLBACK_TRANSITION_DURATION = 0;
var useDialog = (props) => {
  const { ref, isOpen, transition, onOpen, onClose } = props;
  const [hasDialogSupport, setHasDialogSupport] = (0, import_react4.useState)(false);
  const shouldTransition = hasDialogSupport && transition.isEnabled;
  const { polyfillDialog } = useDialogPolyfill();
  (0, import_react4.useEffect)(() => {
    setHasDialogSupport(supportsDialog());
  }, []);
  const TRANSITION_DURATION_IN_MS = ref.current && Number(
    getComputedStyle(ref.current).getPropertyValue(transition.CSSVariable).replace("ms", "")
  ) || FALLBACK_TRANSITION_DURATION;
  (0, import_mobius_hooks2.useBodyScrollLock)({ enabled: isOpen });
  const open = (0, import_react4.useCallback)(() => {
    ref.current?.showModal();
    onOpen?.();
  }, [onOpen, ref]);
  const close = (0, import_react4.useCallback)(() => {
    ref.current?.close();
    onClose?.();
  }, [onClose, ref]);
  const handleClose = (0, import_react4.useCallback)(
    (event) => {
      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }
      if (shouldTransition) {
        ref.current?.classList.remove(TRANSITION_CLASS_NAME);
        setTimeout(() => close(), TRANSITION_DURATION_IN_MS);
      } else {
        close();
      }
    },
    [TRANSITION_DURATION_IN_MS, close, ref, shouldTransition]
  );
  (0, import_react4.useEffect)(() => {
    async function toggleDialog() {
      if (isOpen && !ref.current?.open) {
        await polyfillDialog(ref);
        open();
        setTimeout(() => {
          ref.current?.classList.add(TRANSITION_CLASS_NAME);
        }, 0);
      } else if (!isOpen && ref.current?.open) {
        handleClose();
      }
    }
    void toggleDialog();
  }, [handleClose, isOpen, open, ref, polyfillDialog]);
  return {
    open,
    close: handleClose
  };
};

// src/hooks/useLabel/useLabel.tsx
var import_react5 = require("react");
function useLabel({
  id: providedId,
  label,
  "aria-label": ariaLabel,
  "aria-labelledby": ariaLabelledby,
  labelElementType = "label"
}) {
  let labelProps = {};
  let fieldProps = {};
  const hasWarnedAboutMissingLabels = (0, import_react5.useRef)(false);
  const fallbackId = (0, import_react5.useId)();
  const id = providedId || fallbackId;
  const labelId = (0, import_react5.useId)();
  if (label) {
    ariaLabelledby = ariaLabelledby ? `${labelId} ${ariaLabelledby}` : labelId;
    labelProps = {
      id: labelId,
      htmlFor: labelElementType === "label" ? id : void 0
    };
  } else if (!ariaLabelledby && !ariaLabel && !hasWarnedAboutMissingLabels.current) {
    hasWarnedAboutMissingLabels.current = true;
    console.warn(
      "If you do not provide a visible label, you must specify an aria-label or aria-labelledby attribute for accessibility"
    );
  }
  fieldProps = {
    id,
    "aria-label": ariaLabel,
    "aria-labelledby": ariaLabelledby
  };
  return {
    labelProps,
    fieldProps
  };
}

// src/hooks/useTextField/useTextField.tsx
var import_react6 = require("react");
function useTextField(props) {
  const {
    isDisabled = false,
    isReadOnly = false,
    isRequired = false,
    inputElementType = "input"
  } = props;
  const { labelProps, fieldProps } = useLabel(props);
  const descriptionId = (0, import_react6.useId)();
  const descriptionProps = { id: descriptionId };
  const errorMessageId = (0, import_react6.useId)();
  const errorMessageProps = { id: errorMessageId };
  const ariaDescribedBy = spaceDelimitedList([
    props.description && descriptionId,
    props.errorMessage && errorMessageId,
    props["aria-describedby"]
  ]);
  return {
    descriptionProps,
    errorMessageProps,
    labelProps,
    inputProps: {
      defaultValue: props.defaultValue,
      value: props.value,
      onChange: props.onChange,
      disabled: isDisabled,
      readOnly: isReadOnly,
      required: isRequired,
      "aria-required": isRequired === true ? true : void 0,
      "aria-invalid": props.isInvalid,
      "aria-describedby": ariaDescribedBy,
      "aria-errormessage": props["aria-errormessage"],
      role: props.role,
      type: inputElementType === "input" ? props.type : void 0,
      pattern: inputElementType === "input" ? props.pattern : void 0,
      autoComplete: props.autoComplete,
      maxLength: props.maxLength,
      minLength: props.minLength,
      name: props.name,
      placeholder: props.placeholder,
      inputMode: props.inputMode,
      // Clipboard events
      onCopy: props.onCopy,
      onCut: props.onCut,
      onPaste: props.onPaste,
      // Composition events
      onCompositionEnd: props.onCompositionEnd,
      onCompositionStart: props.onCompositionStart,
      onCompositionUpdate: props.onCompositionUpdate,
      // Selection events
      onSelect: props.onSelect,
      // Input events
      onBeforeInput: props.onBeforeInput,
      onInput: props.onInput,
      // Focus events
      onFocus: props.onFocus,
      onBlur: props.onBlur,
      ...fieldProps
    }
  };
}

// src/hooks/useValidationClasses/useValidationClasses.ts
var useValidationClasses = (props) => {
  const { isInvalid } = props;
  if (isInvalid) {
    return "--is-invalid";
  }
  if (isInvalid === false) {
    return "--is-valid";
  }
  return "";
};
//# sourceMappingURL=index.js.map
