import React from 'react';
import {
  LLCoreWidget,
  LLWidgetFooter,
  LLWidgetHeader,
  useWidgetExpiryEffect,
  useWidgetInteractiveTimeout,
} from '@livelike/react-native';
import {
  LLSocialEmbedWidgetBody,
  LLSocialEmbedWidgetBodyProps,
} from './LLSocialEmbedWidgetBody';
import { WidgetKind } from '@livelike/javascript';
import { LLBaseWidgetProps } from '../LLBaseWidgetProps';

export type LLSocialEmbedWidgetProps = Omit<LLBaseWidgetProps, 'widgetKind'> & {
  BodyComponent?: typeof LLSocialEmbedWidgetBody;
  BodyComponentStyles?: LLSocialEmbedWidgetBodyProps['styles'];
};

export function LLSocialEmbedWidget({
  programId,
  widgetId,
  onDismiss: onDismissProp,
  interactiveTimeout: interactiveTimeoutProp,
  onInteractiveTimeout: onInteractiveTimeoutProp,
  WidgetComponent = LLCoreWidget,
  HeaderComponent = LLWidgetHeader,
  BodyComponent = LLSocialEmbedWidgetBody,
  FooterComponent = LLWidgetFooter,
  WidgetComponentStyles,
  HeaderComponentStyles,
  BodyComponentStyles,
  FooterComponentStyles,
}: LLSocialEmbedWidgetProps) {
  const { interactiveTimeout, onInteractiveTimeout } =
    useWidgetInteractiveTimeout({
      widgetId,
      interactiveTimeout: interactiveTimeoutProp,
      onInteractiveTimeout: onInteractiveTimeoutProp,
    });

  useWidgetExpiryEffect({ widgetId });

  return (
    <WidgetComponent
      widgetId={widgetId}
      widgetKind={WidgetKind.SOCIAL_EMBED}
      programId={programId}
      onDismiss={onDismissProp}
      styles={WidgetComponentStyles}
    >
      {({ widget, onDismiss }) => (
        <>
          <HeaderComponent
            title={widget.comment}
            interactiveTimeout={interactiveTimeout}
            onInteractiveTimeout={onInteractiveTimeout}
            onDismiss={onDismiss}
            dismissable={!!onDismissProp}
            styles={HeaderComponentStyles}
          />
          <BodyComponent widgetId={widgetId} styles={BodyComponentStyles} />
          <FooterComponent styles={FooterComponentStyles} widgetId={widgetId} />
        </>
      )}
    </WidgetComponent>
  );
}
