import type { DmObject } from '../model';
import { lockProps } from './viewOnly';

type Switchable = Pick<DmObject, 'name' | 'active' | 'deactivated'>;

/**
 * How a switched-off object is switched on: a CRM-defined object never switched on opens the
 * Create object panel (HUB-605); an archived custom object or a switched-off standard object is
 * restored. Null for an active object.
 */
export function activateKind(o: Omit<Switchable, 'name'>): 'create' | 'restore' | null {
  if (o.active) return null;
  return o.deactivated ? 'create' : 'restore';
}

/**
 * #563: the inline "Activate" button on a switched-off object's Manage table row, as in
 * HubSpot (the graph card has its own). Nothing for an active object.
 */
export function ActivateObjectButton({
  object: o,
  onActivate,
  canEdit = true,
}: {
  object: Switchable;
  onActivate: () => void;
  /** Off, saying why, for someone who can only view the data model (`viewOnly.ts`). */
  canEdit?: boolean;
}) {
  if (o.active) return null;
  return (
    <button
      type="button"
      className="dm-btn dm-btn--outline dm-btn--xxs"
      aria-label={`Activate ${o.name}`}
      onClick={onActivate}
      {...lockProps(canEdit)}
    >
      Activate
    </button>
  );
}
