import { RichText, Text } from '@tarojs/components';
import { deliveryHintHtmlToRichNodes } from '@dukang/shared-types';
import { DEFAULT_LOCAL_DELIVERY_HINT } from '../lib/local-delivery';

type DeliveryHintHtmlProps = {
  html?: string | null;
  className?: string;
};

/** 承运商配送提示：HTML 用 RichText；textarea 里的换行转成 br */
export default function DeliveryHintHtml({
  html,
  className = '',
}: DeliveryHintHtmlProps) {
  const raw = (html || '').trim() || DEFAULT_LOCAL_DELIVERY_HINT;
  const nodes = deliveryHintHtmlToRichNodes(raw);
  const looksHtml = /<[a-z][\s\S]*>/i.test(nodes);
  if (!looksHtml) {
    return <Text className={className}>{nodes}</Text>;
  }
  return <RichText className={className} nodes={nodes} />;
}
