import { ColorSwatchField, drawerInputClass, drawerTextareaClass, Field, Section, Segmented, Toggle, useScopedFormContext } from '@components/common/page-builder/index.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import type { CouponBorderStyle } from './CouponBlock.js'; interface CouponBlockSettingProps { couponBlockWidget?: { eyebrow?: string | null; heading?: string; body?: string | null; code?: string; ctaLabel?: string | null; ctaLink?: string; ctaNewTab?: boolean | null; expires?: string | null; borderStyle?: CouponBorderStyle | null; backgroundColor?: string | null; }; } const BORDER_OPTIONS: ReadonlyArray<{ value: CouponBorderStyle; label: string; }> = [ { value: 'solid', label: _('Solid') }, { value: 'dashed', label: _('Dashed') }, { value: 'none', label: _('None') } ]; function formatCountdown(iso: string): string { const t = Date.parse(iso); if (!Number.isFinite(t)) return ''; const diff = t - Date.now(); if (diff <= 0) return _('Already expired'); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); if (days >= 1) return _('Expires in ${days}d ${hours}h', { days: String(days), hours: String(hours) }); const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); return _('Expires in ${hours}h ${minutes}m', { hours: String(hours), minutes: String(minutes) }); } export default function CouponBlockSetting({ couponBlockWidget }: CouponBlockSettingProps) { const { eyebrow, heading, body, code, ctaLabel, ctaLink, ctaNewTab, expires, borderStyle, backgroundColor } = couponBlockWidget ?? {}; const { register, setValue, watch } = useScopedFormContext(); const eyebrowV = (watch('settings.eyebrow') as string) ?? eyebrow ?? ''; const headingV = (watch('settings.heading') as string) ?? heading ?? ''; const bodyV = (watch('settings.body') as string) ?? body ?? ''; const codeV = (watch('settings.code') as string) ?? code ?? ''; const ctaLabelV = (watch('settings.ctaLabel') as string) ?? ctaLabel ?? ''; const ctaLinkV = (watch('settings.ctaLink') as string) ?? ctaLink ?? ''; const ctaNewTabV = (watch('settings.ctaNewTab') as boolean | null) ?? ctaNewTab ?? false; const expiresV = (watch('settings.expires') as string) ?? expires ?? ''; const borderStyleV = ((watch('settings.borderStyle') as string) ?? borderStyle ?? 'dashed') as CouponBorderStyle; const backgroundColorV = (watch('settings.backgroundColor') as string) ?? backgroundColor ?? ''; // datetime-local accepts `YYYY-MM-DDTHH:MM`; storage is ISO. Convert both // ways so the input mirrors what's saved. const localExpiresInput = expiresV ? (() => { const d = new Date(expiresV); if (Number.isNaN(d.getTime())) return ''; const pad = (n: number) => String(n).padStart(2, '0'); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad( d.getDate() )}T${pad(d.getHours())}:${pad(d.getMinutes())}`; })() : ''; return (