'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var hooks = require('preact/hooks');

/**
 * Subscribes to the special wrapper type that all "changeable" values in the
 * checkout use. This hook extracts the most recent value from that object,
 * and subscribes to update the value when changes occur in the checkout.
 *
 * > Note:
 * > As of version 2025-10, you no longer need this hook. When you access `.value`
 * > (instead of `.current`) on subscribable properties, Preact will automatically
 * > re-render as `.value` changes.
 */
function useSubscription(subscription) {
  const [, setValue] = hooks.useState(subscription.value);
  hooks.useEffect(() => {
    let didUnsubscribe = false;
    const checkForUpdates = newValue => {
      if (didUnsubscribe) {
        return;
      }
      setValue(newValue);
    };
    const unsubscribe = subscription.subscribe(checkForUpdates);

    // Because we're subscribing in a passive effect,
    // it's possible for an update to occur between render and the effect handler.
    // Check for this and schedule an update if work has occurred.
    checkForUpdates(subscription.value);
    return () => {
      didUnsubscribe = true;
      unsubscribe();
    };
  }, [subscription]);
  return subscription.value;
}

exports.useSubscription = useSubscription;
