import { format } from "../lib/format";
import { text } from "../lib/config";
import { Slot } from "../lib/slots";
import { plainText } from "../lib/plain";
import { Html } from "../components/Html";
import { PinIcon } from "../components/icons";
import { CardRemove } from "./CardRemove";

/**
 * Sticky product strip of the cards view: image, name and price for each
 * visible product. The base product carries a "Pinned" badge (or the
 * table's own label, e.g. "This product"); the others
 * offer "Pin" to become the base. On the shopper's own list each card also
 * has a Remove (✕) button on its image.
 */
export function CardStrip({
  visible,
  base,
  baseLabel,
  canPin,
  onPin,
  canRemove,
}) {
  return (
    <div className="pcfw-cards__strip">
      {visible.map((product) => {
        const isBase = base && product.id === base.id;

        return (
          <div key={product.id} className="pcfw-cards__product">
            <div className="pcfw-cards__media">
              <a className="pcfw-cards__image" href={product.permalink}>
                <img src={product.image} alt="" loading="lazy" />
                {isBase && (
                  <span className="pcfw-cards__badge">
                    <PinIcon size={12} />
                    {baseLabel || text("pinned", "Pinned")}
                  </span>
                )}
              </a>
              <Slot name="bulkPick" product={product} />
              {canRemove && <CardRemove product={product} />}
            </div>
            <a
              className="pcfw-cards__name"
              href={product.permalink}
              dangerouslySetInnerHTML={{ __html: product.name }}
            />
            <Html className="pcfw-cards__price" html={product.price_html} />
            {canPin && !isBase && (
              <button
                type="button"
                className="pcfw-cards__pin"
                onClick={() => onPin(product.id)}
                aria-label={format(
                  text("pinItem", "Pin %s as the base product"),
                  plainText(product.name),
                )}
              >
                <PinIcon size={12} />
                {text("pin", "Pin")}
              </button>
            )}
          </div>
        );
      })}
    </div>
  );
}
