// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import React from 'react';
import {
  ServerAlert,
  type ServerAlertsType,
} from '../types/ServerAlert.std.ts';
import type { WidthBreakpoint } from './_util.std.ts';
import type { LocalizerType } from '../types/I18N.std.ts';
import { CriticalIdlePrimaryDeviceDialog } from './CriticalIdlePrimaryDeviceDialog.dom.tsx';
import { strictAssert } from '../util/assert.std.ts';
import { WarningIdlePrimaryDeviceDialog } from './WarningIdlePrimaryDeviceDialog.dom.tsx';

export function getServerAlertDialog(
  alerts: ServerAlertsType | undefined,
  getServerAlertToShow: (alerts: ServerAlertsType) => ServerAlert | null,
  saveAlerts: (alerts: ServerAlertsType) => Promise<void>,
  dialogProps: {
    containerWidthBreakpoint: WidthBreakpoint;
    i18n: LocalizerType;
  }
): React.JSX.Element | null {
  if (!alerts) {
    return null;
  }
  const alertToShow = getServerAlertToShow(alerts);
  if (!alertToShow) {
    return null;
  }

  if (alertToShow === ServerAlert.CRITICAL_IDLE_PRIMARY_DEVICE) {
    return <CriticalIdlePrimaryDeviceDialog {...dialogProps} />;
  }

  if (alertToShow === ServerAlert.IDLE_PRIMARY_DEVICE) {
    const alert = alerts[ServerAlert.IDLE_PRIMARY_DEVICE];
    strictAssert(alert, 'alert must exist');

    // Only allow dismissing it once
    const isDismissable = alert.dismissedAt == null;

    return (
      <WarningIdlePrimaryDeviceDialog
        {...dialogProps}
        handleClose={
          isDismissable
            ? async () => {
                await saveAlerts({
                  ...alerts,
                  [ServerAlert.IDLE_PRIMARY_DEVICE]: {
                    ...alert,
                    dismissedAt: Date.now(),
                  },
                });
              }
            : undefined
        }
      />
    );
  }

  return null;
}
