import { FC, memo } from 'react';

import { useDrawerV2 } from '@material-hu/hooks/useDrawerV2';

import getScheduleDrawerConfig from '../../feed/components/schedule/components/ScheduleDrawerContent';
import SchedulePostButton from '../../feed/components/schedule/components/SchedulePostButton';
import { useScheduleDrawerState } from '../../feed/components/schedule/components/useScheduleDrawerState';
import { useGroupMember } from '../GroupMemberContext';

type GroupSchedulePostProps = {
  disabled: boolean;
  onSchedulePost: (date: string, canSendPush: boolean) => void;
};

const GroupSchedulePost: FC<GroupSchedulePostProps> = props => {
  const { disabled, onSchedulePost } = props;
  const { isGroupAdmin } = useGroupMember();

  const scheduleDrawerState = useScheduleDrawerState({
    onSchedulePost,
    canSendPush: isGroupAdmin,
  });

  const { drawer, showDrawer } = useDrawerV2(({ closeDrawer }) =>
    getScheduleDrawerConfig(scheduleDrawerState, closeDrawer),
  );

  return (
    <>
      <SchedulePostButton
        disabled={disabled}
        onClick={() => showDrawer({})}
      />
      {drawer}
    </>
  );
};

export default memo(GroupSchedulePost);
