// Copyright 2024 Northern.tech AS
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
import type { FunctionComponent, MouseEvent, ReactNode } from 'react';

import { Close as CloseIcon, Link as LinkIcon } from '@mui/icons-material';
import { IconButton, Tooltip, Typography } from '@mui/material';

export interface DrawerTitleProps {
  onClose: (event: MouseEvent<HTMLButtonElement>) => void;
  onLinkCopy?: () => void;
  postTitle?: ReactNode;
  preCloser?: ReactNode;
  title: ReactNode;
}

export const DrawerTitle: FunctionComponent<DrawerTitleProps> = ({ onClose, onLinkCopy, postTitle, preCloser, title }) => (
  <div className="flexbox margin-bottom-small space-between relative">
    <div className="flexbox align-items-center">
      <Typography className="capitalized-start flexbox align-items-center" component="div" variant="subtitle1">
        {title}
      </Typography>
      {onLinkCopy && (
        <Tooltip title="Copy link">
          <IconButton onClick={onLinkCopy} size="large">
            <LinkIcon />
          </IconButton>
        </Tooltip>
      )}
      {postTitle}
    </div>
    <div className="flexbox align-items-center relative">
      {preCloser}
      <IconButton className="margin-left-small" onClick={onClose} aria-label="close" size="large">
        <CloseIcon />
      </IconButton>
    </div>
  </div>
);
