{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "merchant-hours",
  "title": "Merchant Hours",
  "type": "registry:block",
  "files": [
    {
      "path": "packages/registry-native/blocks/merchant-hours/MerchantHours.tsx",
      "content": "import { useMemo } from 'react'\nimport { View, type ViewProps } from 'react-native'\nimport { Badge } from '@/components/ui/badge'\nimport { Button } from '@/components/ui/button'\nimport { Text } from '@/components/ui/text'\nimport { useAnnouncement } from '@/lib/accessibility'\nimport { cn } from '@/lib/utils'\nimport {\n  merchantDate,\n  merchantWeekdays,\n  resolveMerchantHours,\n  validMerchantWindows,\n  weekdayFor,\n  type MerchantHoursOverrides,\n  type MerchantHoursWindow,\n  type MerchantLocalTime,\n  type MerchantWeeklyHours,\n} from './hours'\n\ninterface MerchantHoursLabels {\n  regular: string\n  special: string\n  open: string\n  closedNow: string\n  unknown: string\n  closes: string\n  opens: string\n  overnight: string\n  closed: string\n  allDay: string\n  directions: string\n}\n\nexport interface MerchantHoursProps extends Omit<ViewProps, 'children'> {\n  className?: string\n  title?: string\n  weekly?: MerchantWeeklyHours\n  overrides?: MerchantHoursOverrides\n  /** Supply this from the location's clock, not from the phone's local wall time. */\n  localNow?: MerchantLocalTime\n  locale?: string\n  timeZoneLabel?: string\n  labels?: Partial<MerchantHoursLabels>\n  onDirections?: () => void\n}\n\nconst noOverrides: MerchantHoursOverrides = {}\n\nfunction dateFormat(locale?: string) {\n  let resolvedLocale: string | undefined\n  try {\n    resolvedLocale = (locale && Intl.getCanonicalLocales(locale)[0]) || undefined\n  } catch {\n    // A malformed optional locale should not obscure the store's posted hours.\n  }\n  return {\n    time: new Intl.DateTimeFormat(resolvedLocale, { timeZone: 'UTC', hour: 'numeric', minute: '2-digit' }),\n    weekday: new Intl.DateTimeFormat(resolvedLocale, { timeZone: 'UTC', weekday: 'long' }),\n    date: new Intl.DateTimeFormat(resolvedLocale, { timeZone: 'UTC', month: 'short', day: 'numeric', year: 'numeric' }),\n  }\n}\n\nexport function MerchantHours({\n  className,\n  title = 'Store hours',\n  weekly,\n  overrides = noOverrides,\n  localNow,\n  locale,\n  timeZoneLabel,\n  labels,\n  onDirections,\n  ...props\n}: MerchantHoursProps) {\n  const format = useMemo(() => dateFormat(locale), [locale])\n  const availability = useMemo(() => resolveMerchantHours(weekly, overrides, localNow), [weekly, overrides, localNow])\n  const today = localNow && merchantDate(localNow.date)\n  const weekday = today ? weekdayFor(today) : undefined\n  const overrideDates = Object.keys(overrides)\n    .map((date) => ({ key: date, date: merchantDate(date) }))\n    .filter((entry): entry is { key: string; date: Date } => Boolean(entry.date && (!today || entry.date >= today)))\n    .sort((first, second) => first.date.valueOf() - second.date.valueOf())\n\n  function time(minute: number) {\n    return format.time.format(new Date(Date.UTC(2024, 0, 1, 0, minute)))\n  }\n\n  function describeMoment(moment: MerchantLocalTime) {\n    if (moment.date === localNow?.date) return time(moment.minute)\n    const date = merchantDate(moment.date)\n    return `${date ? format.date.format(date) : moment.date}, ${time(moment.minute)}`\n  }\n\n  function describeWindows(windows: readonly MerchantHoursWindow[] | undefined) {\n    const sorted = validMerchantWindows(windows)\n    if (sorted.length === 0) return labels?.closed ?? 'Closed'\n    if (sorted.some(({ opens, closes }) => opens === 0 && closes === 1440)) return labels?.allDay ?? 'Open 24 hours'\n    return sorted\n      .map(({ opens, closes }) => {\n        const overnight = closes < opens || closes === 1440\n        return `${time(opens)}–${time(closes)}${overnight ? ` (${labels?.overnight ?? 'next day'})` : ''}`\n      })\n      .join('\\n')\n  }\n\n  const status =\n    availability.state === 'open'\n      ? (labels?.open ?? 'Open now')\n      : availability.state === 'closed'\n        ? (labels?.closedNow ?? 'Closed now')\n        : (labels?.unknown ?? 'Current hours unavailable')\n  const transition =\n    availability.state === 'open' && availability.closes\n      ? `${labels?.closes ?? 'Closes at'} ${describeMoment(availability.closes)}`\n      : availability.state === 'closed' && availability.next\n        ? `${labels?.opens ?? 'Opens at'} ${describeMoment(availability.next)}`\n        : undefined\n  useAnnouncement(transition ? `${status}. ${transition}` : status)\n\n  return (\n    <View {...props} className={cn('gap-[16px]', className)}>\n      <View className=\"gap-[4px]\">\n        <Text size=\"title\" accessibilityRole=\"header\">\n          {title}\n        </Text>\n        {timeZoneLabel ? (\n          <Text size=\"sm\" variant=\"muted\">\n            {timeZoneLabel}\n          </Text>\n        ) : null}\n      </View>\n      <View className=\"gap-[8px] rounded-lg border border-border bg-card p-[16px]\">\n        <View className=\"items-start\">\n          <Badge variant={availability.state === 'open' ? 'success' : 'secondary'}>{status}</Badge>\n        </View>\n        {transition ? <Text className=\"font-medium text-card-foreground\">{transition}</Text> : null}\n      </View>\n      {overrideDates.length ? (\n        <View className=\"gap-[8px]\">\n          <Text accessibilityRole=\"header\" className=\"font-semibold\">\n            {labels?.special ?? 'Special hours'}\n          </Text>\n          {overrideDates.map(({ key, date }) => (\n            <View key={key} className=\"gap-[4px] border-b border-border py-[8px]\">\n              <Text className=\"font-medium\">{format.date.format(date)}</Text>\n              {overrides[key].label ? (\n                <Text size=\"sm\" variant=\"muted\">\n                  {overrides[key].label}\n                </Text>\n              ) : null}\n              <Text size=\"sm\">{describeWindows(overrides[key].windows)}</Text>\n            </View>\n          ))}\n        </View>\n      ) : null}\n      {weekly ? (\n        <View className=\"gap-[8px]\">\n          <Text accessibilityRole=\"header\" className=\"font-semibold\">\n            {labels?.regular ?? 'Regular hours'}\n          </Text>\n          {merchantWeekdays.map((day, index) => (\n            <View\n              key={day}\n              className=\"flex-row flex-wrap items-start justify-between gap-[8px] border-b border-border py-[8px]\"\n            >\n              <Text className={cn('min-w-[100px] shrink', weekday === day && 'font-semibold')}>\n                {format.weekday.format(new Date(Date.UTC(2024, 0, index + 1)))}\n              </Text>\n              <Text size=\"sm\" variant=\"muted\" className=\"shrink\">\n                {describeWindows(weekly[day])}\n              </Text>\n            </View>\n          ))}\n        </View>\n      ) : null}\n      {onDirections ? (\n        <Button variant=\"outline\" onPress={onDirections}>\n          {labels?.directions ?? 'Get directions'}\n        </Button>\n      ) : null}\n    </View>\n  )\n}\n",
      "type": "registry:block",
      "target": "~/components/blocks/merchant-hours/MerchantHours.tsx"
    },
    {
      "path": "packages/registry-native/blocks/merchant-hours/hours.ts",
      "content": "export const merchantWeekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'] as const\n\nexport type MerchantWeekday = (typeof merchantWeekdays)[number]\n\n/** Minutes from local midnight. A closing minute below opening continues into the next day. */\nexport interface MerchantHoursWindow {\n  opens: number\n  closes: number\n}\n\nexport type MerchantWeeklyHours = Partial<Record<MerchantWeekday, readonly MerchantHoursWindow[]>>\n\nexport interface MerchantHoursOverride {\n  label?: string\n  /** Replaces the entire local date, including any window that would otherwise continue from yesterday. */\n  windows: readonly MerchantHoursWindow[]\n}\n\nexport type MerchantHoursOverrides = Readonly<Record<string, MerchantHoursOverride>>\n\nexport interface MerchantLocalTime {\n  /** Calendar date at the business, YYYY-MM-DD. */\n  date: string\n  /** The business's local clock, expressed in minutes since midnight. */\n  minute: number\n}\n\nexport type MerchantHoursAvailability =\n  { state: 'unknown' } | { state: 'open'; closes?: MerchantLocalTime } | { state: 'closed'; next?: MerchantLocalTime }\n\nconst MINUTES_PER_DAY = 1440\nconst MILLIS_PER_DAY = 86_400_000\n\nexport function merchantDate(date: string): Date | undefined {\n  if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(date)) return\n  // UTC is a calendar-arithmetic carrier. No device time zone conversion is performed.\n  const calendarDay = new Date(`${date}T00:00:00Z`)\n  if (Number.isFinite(calendarDay.valueOf()) && isoDate(calendarDay) === date) return calendarDay\n}\n\nfunction isoDate(day: Date) {\n  return day.toISOString().split('T')[0]\n}\n\nexport function weekdayFor(day: Date): MerchantWeekday {\n  return merchantWeekdays[(day.getUTCDay() + 6) % 7]\n}\n\nexport function validMerchantWindows(windows: readonly MerchantHoursWindow[] | undefined) {\n  return (windows ?? [])\n    .filter(\n      ({ opens, closes }) =>\n        Number.isInteger(opens) &&\n        Number.isInteger(closes) &&\n        opens >= 0 &&\n        opens < MINUTES_PER_DAY &&\n        closes >= 0 &&\n        closes <= MINUTES_PER_DAY &&\n        opens !== closes,\n    )\n    .sort((first, second) => first.opens - second.opens)\n}\n\nfunction offsetDate(day: Date, offset: number) {\n  return new Date(day.valueOf() + offset * MILLIS_PER_DAY)\n}\n\nfunction toLocalTime(day: Date, offsetMinutes: number): MerchantLocalTime {\n  const offset = Math.floor(offsetMinutes / MINUTES_PER_DAY)\n  return { date: isoDate(offsetDate(day, offset)), minute: offsetMinutes - offset * MINUTES_PER_DAY }\n}\n\nexport function resolveMerchantHours(\n  weekly: MerchantWeeklyHours | undefined,\n  overrides: MerchantHoursOverrides,\n  localNow: MerchantLocalTime | undefined,\n): MerchantHoursAvailability {\n  const today = localNow && merchantDate(localNow.date)\n  if (\n    !weekly ||\n    !localNow ||\n    !today ||\n    !Number.isInteger(localNow.minute) ||\n    localNow.minute < 0 ||\n    localNow.minute >= MINUTES_PER_DAY\n  ) {\n    return { state: 'unknown' }\n  }\n\n  const normalHours = merchantWeekdays.map((weekday) => validMerchantWindows(weekly[weekday]))\n  const recurring = normalHours.some((windows) => windows.length > 0)\n  const overrideOffsets = Object.keys(overrides)\n    .map(merchantDate)\n    .filter((date): date is Date => Boolean(date && date >= today))\n    .map((date) => (date.valueOf() - today.valueOf()) / MILLIS_PER_DAY)\n  // Each exception can suppress at most one occurrence of a recurring weekday.\n  const horizon = 7 * (overrideOffsets.length + 1)\n  const offsets = recurring\n    ? Array.from({ length: horizon + 2 }, (_, index) => index - 1)\n    : [...new Set([-1, 0, ...overrideOffsets])]\n  const intervals = offsets\n    .flatMap((offset) => {\n      const day = offsetDate(today, offset)\n      const override = overrides[isoDate(day)]\n      const windows = override ? validMerchantWindows(override.windows) : normalHours[(day.getUTCDay() + 6) % 7]\n      return windows.map(({ opens, closes }) => ({\n        start: offset * MINUTES_PER_DAY + opens,\n        end:\n          offset * MINUTES_PER_DAY +\n          (closes > opens\n            ? closes\n            : overrides[isoDate(offsetDate(day, 1))]\n              ? MINUTES_PER_DAY\n              : MINUTES_PER_DAY + closes),\n      }))\n    })\n    .sort((first, second) => first.start - second.start)\n\n  const merged: typeof intervals = []\n  for (const interval of intervals) {\n    const last = merged.at(-1)\n    if (last && interval.start <= last.end) last.end = Math.max(last.end, interval.end)\n    else merged.push(interval)\n  }\n\n  const current = merged.find(({ start, end }) => start <= localNow.minute && localNow.minute < end)\n  if (current) {\n    // The artificial end of a 24/7 forecast is not a real closing time.\n    return recurring && current.end >= (horizon + 1) * MINUTES_PER_DAY\n      ? { state: 'open' }\n      : { state: 'open', closes: toLocalTime(today, current.end) }\n  }\n  const next = merged.find(({ start }) => start > localNow.minute)\n  return next ? { state: 'closed', next: toLocalTime(today, next.start) } : { state: 'closed' }\n}\n",
      "type": "registry:block",
      "target": "~/components/blocks/merchant-hours/hours.ts"
    },
    {
      "path": "packages/registry-native/blocks/merchant-hours/index.ts",
      "content": "export { MerchantHours, type MerchantHoursProps } from './MerchantHours'\nexport { resolveMerchantHours } from './hours'\nexport type {\n  MerchantHoursAvailability,\n  MerchantHoursOverride,\n  MerchantHoursOverrides,\n  MerchantHoursWindow,\n  MerchantLocalTime,\n  MerchantWeekday,\n  MerchantWeeklyHours,\n} from './hours'\n",
      "type": "registry:block",
      "target": "~/components/blocks/merchant-hours/index.ts"
    }
  ],
  "dependencies": [],
  "devDependencies": [],
  "registryDependencies": [
    "https://native.uipkge.dev/r/native/accessibility.json",
    "https://native.uipkge.dev/r/native/badge.json",
    "https://native.uipkge.dev/r/native/button.json",
    "https://native.uipkge.dev/r/native/text.json",
    "https://native.uipkge.dev/r/native/utils.json"
  ],
  "description": "Weekly opening hours with breaks, overnight windows, dated overrides and a local current status.",
  "categories": [
    "commerce"
  ]
}