"use strict";

import cn from "classnames";
import React from "react";

import closeIcon from "!!svg-sprite-loader!@cloudbees/honeyui-icons/svg/close.svg";

import { Icon } from "../";

import Link from "./link";
import Header from "./header";

type AlertVariants =
  | "primary"
  | "secondary"
  | "success"
  | "danger"
  | "warning"
  | "info"
  | "dark"
  | "light";

interface IAlertProps {
  className?: string;
  dismissible?: boolean;
  variant?: AlertVariants;
  style?: object;
  "data-testid"?: string;
  onClose?: () => void;
}

interface IAlertStatic {
  Link: typeof Link;
  Header: typeof Header;
}

const Alert: React.FC<IAlertProps> & IAlertStatic = props => {
  const {
    className,
    dismissible,
    variant,
    children,
    style,
    "data-testid": testId,
    onClose,
    ...restProps
  } = props;

  return (
    <div
      role="alert"
      className={cn(
        "alert",
        { [`alert-${variant}`]: variant, "alert-dismissible": dismissible },
        className
      )}
      data-testid={testId}
      style={style}
      {...restProps}
    >
      {dismissible && (
        <button
          type="button"
          className="close d-flex p-3"
          data-testid="honeyui-alert-close"
          onClick={onClose}
        >
          <Icon
            {...{ id: closeIcon.id, viewBox: closeIcon.viewBox, size: "sm", currentColor: true }}
          />
        </button>
      )}
      {children}
    </div>
  );
};

Alert.displayName = "Alert";

Alert.defaultProps = {
  variant: "primary",
  "data-testid": "honeyui-alert"
};

Alert.Link = Link;
Alert.Header = Header;

export default Alert;
