import { useCallback } from 'react';

import { IBaseMessageProps } from '@/components/message-chat/base';
import { PageUrl } from '@/constants/app';
import { IJobMessage } from '@/types/message';
import { getScrollItemId, safeJsonParse } from '@/utils/common';
import { getJobSalary } from '@/utils/job';
import { navigateTo } from '@/utils/route';

import './index.less';

interface IProps extends IBaseMessageProps {}

const PREFIX = 'job-message';

function JobMessage(props: IProps) {
  const { id, message } = props;
  const data = safeJsonParse<IJobMessage>(message.actionObject, null);

  const handleClick = useCallback(() => {
    if (!data) {
      return;
    }
    navigateTo(PageUrl.JobDetail, { id: data.id });
  }, [data]);

  if (!data) {
    return null;
  }
  return (
    <div className={PREFIX} id={getScrollItemId(id)} onClick={handleClick}>
      <div className={`${PREFIX}__title`}>{data.title}</div>
      <div className={`${PREFIX}__salary`}>{getJobSalary(data)}</div>
    </div>
  );
}

export default JobMessage;
