import { toTitleCase, toPlural } from '@vizality/util/string';
import { Messages } from '@vizality/i18n';
import React, { memo } from 'react';

import { AddonInfoMessage } from '.';
import { Confirm } from '..';

/**
 * Confirmation modal that appears when uninstalling an addon.
 * @component
 * @param {object} props
 * @param {object} props.addon Addon
 * @param {string} props.type Addon type
 */
export default memo(({ onClose, addon, type, ...props }) => {
  return (
    <Confirm
      {...props}
      header={Messages.VIZALITY_ADDON_UNINSTALL.format({ type: toTitleCase(type) })}
      confirmText={Messages.VIZALITY_UNINSTALL}
      cancelText={Messages.CANCEL}
      onCancel={onClose}
      onConfirm={async () => {
        onClose();
        await vizality.manager[toPlural(type)]?._uninstall(addon.addonId);
      }}
    >
      <AddonInfoMessage message={Messages.VIZALITY_ADDON_UNINSTALL_SURE.format({ type })} addon={addon} />
    </Confirm>
  );
});
