import {
  CustomModalLayout,
  CustomModalLayoutProps,
  ModalProps,
  Text,
  TableActionCellSecondaryAction,
} from '@wix/design-system';
import React from 'react';
import { observer } from 'mobx-react-lite';
import { ModalWrapper } from '../CollectionModal';
import { ConditionalModalState } from '@wix/bex-core';

export interface CustomActionModalProps
  extends Partial<CustomModalLayoutProps> {
  state: ConditionalModalState<unknown>;
  onAfterClose?: ModalProps['onAfterClose'];
  actionProps?: Partial<TableActionCellSecondaryAction>;
}

function _CustomActionModal({
  state,
  content,
  onAfterClose,
  primaryButtonOnClick,
  secondaryButtonOnClick,
  onCloseButtonClick,
  ...rest
}: CustomActionModalProps) {
  const { modal } = state;

  return (
    <ModalWrapper
      state={modal}
      onAfterClose={() => {
        state.afterClose();
        onAfterClose?.();
      }}
    >
      <CustomModalLayout
        dataHook="collection-action-modal"
        {...rest}
        onCloseButtonClick={(e) => {
          modal.close();
          onCloseButtonClick?.(e);
        }}
        primaryButtonOnClick={() => {
          modal.confirm();
          primaryButtonOnClick?.();
        }}
        secondaryButtonOnClick={
          rest.secondaryButtonText
            ? () => {
                modal.decline();
                secondaryButtonOnClick?.();
              }
            : undefined
        }
        content={typeof content === 'string' ? <Text>{content}</Text> : content}
      />
    </ModalWrapper>
  );
}

export const CustomActionModal = observer(_CustomActionModal);
