import { cn } from '@/lib/utils/cn';
import { limitBarWidth, limitPct, limitValueNow } from '../limits';
import { CUSTOM_OBJECT_LIMIT, customObjectCount, fmt, type DataModel } from '../model';

/** "X of Y", "N% Used" and a bar: the design's bar(used, max, wide) (data-model.js:3297-3300). */
export function LimitBar({ used, max, wide }: { used: number; max: number; wide?: boolean }) {
  return (
    <div className={cn('dm-usage', wide && 'dm-usage--wide')}>
      <div className="dm-usage__top">
        <span>
          {fmt(used)} of {fmt(max)}
        </span>
        <b>{limitPct(used, max)} Used</b>
      </div>
      <div
        className="dm-usage__bar"
        role="progressbar"
        aria-valuenow={limitValueNow(used, max)}
        aria-valuemin={0}
        aria-valuemax={100}
      >
        <span style={{ width: `${limitBarWidth(used, max)}%` }} />
      </div>
    </div>
  );
}

/**
 * Limits > Custom objects: the 10-custom-object limit, as the API counts and enforces it
 * (HUB-717), else counted as Create does (customObjectCount). The design's "Upgrade" button is
 * left out (QA #362: no upsell).
 */
export function CustomObjectDefinition({ model }: { model: DataModel }) {
  const limit = model.limits?.customObjects;
  return (
    <div className="dm-defcard">
      <div className="dm-defcard__top">
        <b>Custom Object Definition</b>
      </div>
      <LimitBar
        used={limit?.used ?? customObjectCount(model)}
        max={limit?.max ?? CUSTOM_OBJECT_LIMIT}
        wide
      />
    </div>
  );
}
