import React, { useCallback, useContext, useEffect, useRef } from "react";
import { findNodeHandle } from "react-native";
import { INativeAdContext, NativeAdContext } from "./context";
import { Text, TextProps } from "@momo-kits/foundation";

const TaglineView = ({...props}: TextProps) => {
  const { nativeAd, nativeAdView } = useContext<INativeAdContext>(NativeAdContext);
  const taglineRef = useRef<any>(null);
  const _onLayout = useCallback(() => {
    if (!nativeAdView) return;

    let handle = findNodeHandle(taglineRef.current);
    nativeAdView.setNativeProps({
      tagline: handle,
    });
  }, [nativeAdView, taglineRef]);

  useEffect(() => {
    _onLayout();
  }, [nativeAd, nativeAdView]);

  return (
    <Text ref={taglineRef} onLayout={_onLayout} {...props}>
      {nativeAd ? nativeAd.tagline : null}
    </Text>
  );
};

export default TaglineView;
