import { Controller } from '@hotwired/stimulus' import { formatPrice } from '../utils/format-price' export default class extends Controller { static targets = ['price', 'image', 'swatch', 'colorName', 'stockError'] static values = { variants: Array, selected: Number, stockCheckUrl: String, // Two pre-rendered translated templates (see product/show.blade.php) // rather than one — this controller doesn't reimplement Laravel's // pluralization rules, it just picks whichever of these two the // count actually needs and fills in the number. stockErrorOne: String, stockErrorMany: String, } connect() { const params = new URLSearchParams(window.location.search) const urlId = parseInt(params.get('variant')) const defaultId = this.variantsValue[0]?.id this.selectedValue = urlId && this.variantsValue.find(v => v.id === urlId) ? urlId : defaultId // Capture phase, on this controller's own root element (an ancestor // of the checkout module's add-to-cart