/**
 * Layout and UI constants for PixieSprout.
 */
import { useLayoutEffect, useRef } from 'react';
import { NativeModules } from 'react-native';
import type { CareTaskNotificationQuickAction } from '../types/garden';

// ---- Navigation geometry ----
export const navAspect = 443 / 1876;
export const navShellAspect = 356 / 2317;

// ---- Header offsets ----
export const headerActionOffsetX = -8;
export const headerNotificationOffsetX = -10;
export const headerActionTop = 0;

// ---- Copy ----
export const kitProductTrustMessage =
  'Always check the original product label. Pixie may help summarize information, but manufacturer instructions and safety guidance should take priority.';

// ---- App version (L8) — single source of truth for export manifests. Keep in
// sync with app.json "version". Previously hardcoded '1.0.111' in 2 places.
export const APP_VERSION = '1.0.116';

// ---- Safe area insets ----
export const minStableBottomInset = 8;
export const maxStableBottomInset = 52;

// ---- Edge-to-edge native module ----
type EdgeToEdgeModule = {
  setScanMode?: (enabled: boolean) => void;
};

function isEdgeToEdgeModule(value: unknown): value is EdgeToEdgeModule {
  if (value === null || (typeof value !== 'object' && typeof value !== 'function')) {
    return false;
  }

  const setScanMode = (value as { setScanMode?: unknown }).setScanMode;
  return setScanMode === undefined || typeof setScanMode === 'function';
}

const nativeEdgeToEdge: unknown = NativeModules.EdgeToEdge;
export const edgeToEdge:
  | { setScanMode?: (enabled: boolean) => void }
  | undefined = isEdgeToEdgeModule(nativeEdgeToEdge) ? nativeEdgeToEdge : undefined;

// ---- Gamification ----
export const userLevel = 23;
export const currentXp = 1250;
export const maxXp = 2000;

// ---- Debug ----
export const showCareValidationReset = typeof __DEV__ !== 'undefined' && __DEV__;

// ---- Care task quick actions ----
export const dueTodayQuickActions: CareTaskNotificationQuickAction[] = [
  'complete',
  'snooze',
  'remind_later',
  'remind_after_work',
  'preferred_reminder_time',
];

export const futureTaskQuickActions: CareTaskNotificationQuickAction[] = [
  'remind_later',
  'preferred_reminder_time',
];

function clampStableBottomInset(bottomInset: number): number {
  if (typeof bottomInset !== 'number' || Number.isNaN(bottomInset)) {
    return minStableBottomInset;
  }

  return Math.min(Math.max(bottomInset, minStableBottomInset), maxStableBottomInset);
}

// ---- Stable bottom inset hook ----
export function useStableBottomInset(bottomInset: number): number {
  const clampedBottomInset = clampStableBottomInset(bottomInset);
  const stableInsetRef = useRef(clampedBottomInset);
  const stableInset = Math.max(
    clampStableBottomInset(stableInsetRef.current),
    clampedBottomInset,
  );

  // Commit the observed maximum after render. This preserves the monotonic
  // behavior without mutating a ref during a render that React may abandon.
  useLayoutEffect(() => {
    stableInsetRef.current = stableInset;
  }, [stableInset]);

  return stableInset;
}
