import React from 'react';
import {isURL} from '@ywfe/utils';
import {YLinkProps} from './router.interface';
import {useHistory} from './useHistory';


export const YLink: React.FC<YLinkProps> = ({ goto = '', to = '', target = '_self', children }) => {
  const history = useHistory();

  const linkTo = (event: React.MouseEvent<HTMLElement>) => {
    event.preventDefault();
    if(to) {
      history.push(to);
    } else {
      history.goto(goto);
    }


  }
  return (
    <>
      {
        isURL(to) ? (
          <a href={to} target={target}>{children}</a>
        ) : (
          <a onClick={linkTo} target={target}>{children}</a>
        )
      }
    </>
  );
};
