import React, { useEffect, useState } from 'react';
import { PHXButton } from '../Button';
import { classNames } from '../types';
import { ArrowLeftIcon } from '@heroicons/react/20/solid';
import { PHXBadge } from '../Badge';
import { PHXModal } from '../Modal';
import ImportModal from './ModalImport';
import { PHXBreadcrumb } from '../Breadcrumb';
export const PHXFormWrap = ({ isExport, exportText, onExport, badge, cancelText, children, contentOfModal, className, deleted, secondaryLeftButton, secondaryLeftButtonSubmit, secondaryLeftTextButton, deletedLoading, footerAction = true, headerAction = true, disableHeaderAction = false, headerActionText, headerActionType = 'custom', loading, method, nameOfModal, onSecondary, onCancel, onDelete, onSubmit, onImport, onDanger, onChangeCapture, onHeaderActionClick, onSubmitSecondaryLeftButton, onDeletedSecondaryLeftButton, onlyDelete, size = 'large', formType, submit, submitText, title, type = 'default', backAction = true, breadcrumbItems = [], disableSubmit = false, deletedText = 'Xoá bản ghi', dangerText, showDangerButton, hideCancelButton, multiBadge, }) => {
    const sizeClassName = classNames(size === 'small' && 'mx-auto md:w-5/6 xl:w-1/2', size === 'medium' && 'mx-auto md:w-5/6 lg:w-3/4', size === 'large' && 'mx-auto md:w-md lg:w-11/12');
    const [isModalOpen, setIsModalOpen] = useState(false);
    const [openImport, setOpenImport] = useState(false);
    const hasBreadcrumb = breadcrumbItems.length > 0;
    const shouldShowBackAction = backAction;
    const handleDelete = () => {
        if (onDelete) {
            onDelete();
        }
        else {
            return;
        }
    };
    const handleHeaderAction = () => {
        if (headerActionType === 'import') {
            setOpenImport(true);
        }
        else {
            if (onHeaderActionClick) {
                onHeaderActionClick();
            }
        }
    };
    useEffect(() => {
        setIsModalOpen(deletedLoading);
    }, [deletedLoading]);
    return (React.createElement("div", { className: className },
        React.createElement("form", { method: method, onSubmit: onSubmit },
            React.createElement("div", { className: sizeClassName },
                headerAction && (React.createElement("div", { className: 'mb-4' },
                    hasBreadcrumb && (React.createElement("div", { className: 'mb-1' },
                        React.createElement(PHXBreadcrumb, { items: breadcrumbItems }))),
                    React.createElement("div", { className: 'flex items-center justify-between' },
                        React.createElement("div", { className: 'flex items-center truncate' },
                            shouldShowBackAction && (React.createElement("button", { className: 'inline-flex items-center rounded-lg border-gray-300 bg-transparent px-2 py-1 text-xs font-medium text-gray-700 hover:bg-gray-200 active:bg-gray-300 active:pb-[0.2rem] active:pt-[0.3rem] active:shadow-[0rem_0.125rem_0.1rem_0rem_#0004_inset]', onClick: onCancel, type: 'button' },
                                React.createElement(ArrowLeftIcon, { className: 'h-5 w-5' }))),
                            React.createElement("div", { className: classNames('min-w-0', type === 'default' || type === 'breadcrumb' ? 'flex-1' : 'sm:flex', shouldShowBackAction ? 'ml-2' : '') },
                                React.createElement("h2", { className: classNames('sm:text-md truncate text-lg font-medium leading-6 text-gray-900 sm:tracking-tight', type === 'default' ? '' : 'mr-2 flex-1') }, title),
                                type === 'title-with-badge' && (React.createElement("div", { className: 'sm:flex gap-x-2 hidden' },
                                    badge && (React.createElement("div", { className: 'mt-1 flex items-center sm:mt-0' },
                                        React.createElement(PHXBadge, { text: badge.badgeText, type: badge.badgeType }))),
                                    multiBadge && (React.createElement(React.Fragment, null, multiBadge.map((item, index) => (React.createElement("div", { className: 'mt-1 flex items-center sm:mt-0', key: index },
                                        React.createElement(PHXBadge, { text: item.text, type: item.type })))))))))),
                        (exportText || headerActionText) && (React.createElement("div", { className: 'flex items-center' },
                            isExport && exportText && (React.createElement("div", { className: classNames(headerActionText ? 'mr-2' : '') },
                                React.createElement(PHXButton, { onClick: onExport, secondary: true, size: 'small' }, exportText))),
                            headerActionText && (React.createElement("div", null,
                                React.createElement(PHXButton, { onClick: handleHeaderAction, primary: true, size: 'small', disabled: disableHeaderAction }, headerActionText)))))),
                    type === 'title-with-badge' && (React.createElement("div", { className: 'flex sm:hidden gap-x-2' },
                        badge && (React.createElement("div", { className: 'mt-1 flex items-center sm:mt-0' },
                            React.createElement(PHXBadge, { text: badge.badgeText, type: badge.badgeType }))),
                        multiBadge && (React.createElement(React.Fragment, null, multiBadge.map((item, index) => (React.createElement("div", { className: 'mt-1 flex items-center sm:mt-0', key: index },
                            React.createElement(PHXBadge, { text: item.text, type: item.type })))))))))),
                children,
                React.createElement("div", { className: footerAction
                        ? classNames(deleted || secondaryLeftButton ? 'justify-between' : 'justify-end gap-x-3', 'mt-6 flex items-center pt-6')
                        : 'hidden' },
                    deleted && (React.createElement(React.Fragment, null,
                        React.createElement(PHXButton, { danger: true, size: 'small', onClick: () => setIsModalOpen(true) }, deletedText),
                        React.createElement(PHXModal, { dangerActionText: 'X\u00E1c nh\u1EADn x\u00F3a', dangerLoading: deletedLoading, onDangerClick: handleDelete, primaryActionText: '', onHide: () => setIsModalOpen(false), show: isModalOpen === true, title: `Xoá ${nameOfModal}?` },
                            React.createElement("div", { className: 'sm:flex sm:items-start' },
                                React.createElement("p", { className: 'text-xs text-gray-600' }, contentOfModal ? (contentOfModal) : (React.createElement(React.Fragment, null,
                                    "B\u1EA1n c\u00F3 ch\u1EAFc ch\u1EAFn mu\u1ED1n x\u00F3a b\u1EA3n ghi ",
                                    React.createElement("span", { className: 'font-semibold' }, nameOfModal),
                                    "? Thao t\u00E1c n\u00E0y kh\u00F4ng th\u1EC3 ho\u00E0n t\u00E1c."))))))),
                    secondaryLeftButton && (React.createElement("div", { className: 'flex justify-end gap-x-3' }, secondaryLeftButtonSubmit ? (React.createElement(PHXButton, { size: 'small', loading: loading, onClick: onSubmitSecondaryLeftButton, primary: true }, secondaryLeftTextButton)) : (React.createElement(PHXButton, { danger: true, size: 'small', onClick: onDeletedSecondaryLeftButton }, secondaryLeftTextButton)))),
                    !onlyDelete && (React.createElement("div", { className: 'flex justify-end gap-x-3' },
                        !hideCancelButton && (React.createElement(PHXButton, { size: 'small', secondary: true, onClick: onSecondary ? onSecondary : onCancel }, formType === 'custom' ? cancelText : 'Huỷ thay đổi')),
                        showDangerButton && (React.createElement(PHXButton, { size: 'small', danger: true, onClick: onDanger, loading: loading, disabled: disableSubmit }, dangerText !== null && dangerText !== void 0 ? dangerText : (formType === 'custom' ? 'Từ chối' : dangerText || 'Từ chối'))),
                        submit ? (React.createElement(PHXButton, { disabled: disableSubmit, size: 'small', loading: loading, submit: true, primary: true }, formType === 'custom' ? submitText : formType === 'add' ? 'Thêm mới' : 'Cập nhật')) : (React.createElement(PHXButton, { disabled: disableSubmit, size: 'small', loading: loading, onClick: onSubmit, primary: true }, formType === 'custom' ? submitText : formType === 'add' ? 'Thêm mới' : 'Cập nhật'))))))),
        React.createElement(ImportModal, { show: openImport, onHide: () => setOpenImport(false), handleImport: onImport, onChangeCapture: onChangeCapture })));
};
//# sourceMappingURL=FormWrap.js.map