"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/editor/src/components/sync-connection-error-modal/use-retry-countdown.ts
var use_retry_countdown_exports = {};
__export(use_retry_countdown_exports, {
  useRetryCountdown: () => useRetryCountdown
});
module.exports = __toCommonJS(use_retry_countdown_exports);
var import_element = require("@wordpress/element");
function useRetryCountdown(connectionStatus) {
  const [secondsRemaining, setSecondsRemaining] = (0, import_element.useState)();
  const hasRetriedRef = (0, import_element.useRef)(false);
  (0, import_element.useEffect)(() => {
    if (!connectionStatus) {
      return;
    }
    if ("connected" === connectionStatus.status) {
      setSecondsRemaining(void 0);
      hasRetriedRef.current = false;
      return;
    }
    if ("disconnected" !== connectionStatus.status || !connectionStatus.willAutoRetryInMs) {
      return;
    }
    const { willAutoRetryInMs: retryInMs } = connectionStatus;
    const retryAt = Date.now() + retryInMs;
    const hasRetried = hasRetriedRef.current;
    hasRetriedRef.current = true;
    if (hasRetried) {
      setSecondsRemaining(0);
    }
    let countdownIntervalId = null;
    const startCountdown = () => {
      setSecondsRemaining(Math.ceil((retryAt - Date.now()) / 1e3));
      countdownIntervalId = setInterval(() => {
        const remaining = Math.ceil((retryAt - Date.now()) / 1e3);
        setSecondsRemaining(Math.max(0, remaining));
        if (remaining <= 0 && countdownIntervalId) {
          clearInterval(countdownIntervalId);
        }
      }, 1e3);
    };
    const retryingDelayId = hasRetried ? setTimeout(startCountdown, 500) : null;
    if (!retryingDelayId) {
      startCountdown();
    }
    return () => {
      if (retryingDelayId) {
        clearTimeout(retryingDelayId);
      }
      if (countdownIntervalId) {
        clearInterval(countdownIntervalId);
      }
    };
  }, [connectionStatus]);
  return {
    onManualRetry: () => {
      setSecondsRemaining(0);
    },
    secondsRemaining
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useRetryCountdown
});
//# sourceMappingURL=use-retry-countdown.cjs.map
