import React from "react";
import OldModal, { IProps as IPropsModal } from "..";
import Button, { EButtonVariant } from "../../Button";
import classes from "./classes.module.scss";

type IProps = IPropsModal & {
	closeText: string | JSX.Element;
};

type IState = {
	isOpen: boolean;
};
export default class Alert extends React.Component<IProps, IState> {
	static defaultProps = {
		closeText: "Ok",
		...OldModal.defaultProps,
	};

	constructor(props: IProps) {
		super(props);
		this.state = {
			isOpen: this.props.isOpen ?? true,
		};
		this.onClose = this.onClose.bind(this);
	}

	public override render(): JSX.Element | null {
		return (
			<OldModal
				closeBtn={this.props.closeBtn}
				isOpen={this.state.isOpen}
				onClose={this.onClose}
				header={this.props.header}
				footer={this.footer()}>
				{this.props.children}
			</OldModal>
		);
	}

	private footer(): JSX.Element {
		return (
			<div className={classes["button-container"]}>
				<Button variant={EButtonVariant.SECONDARY} onClick={this.onClose}>
					{this.props.closeText}
				</Button>
			</div>
		);
	}

	private onClose() {
		this.setState({ isOpen: false });
		this.props.onClose?.();
	}
}
