$plan * @return list */ public static function sections(array $plan): array { $found = []; foreach ($plan['pages'] ?? [] as $page) { foreach ($page['sections'] ?? [] as $section) { if (($section['type'] ?? null) === 'hero') { $found[] = [ 'page_id' => (string) ($page['id'] ?? ''), 'page_title' => (string) ($page['title'] ?? ''), 'section_id' => (string) ($section['id'] ?? ''), 'headline' => (string) ($section['content']['headline'] ?? ''), 'variant' => (string) ($section['variant'] ?? ''), 'image' => is_string($section['content']['image'] ?? null) ? $section['content']['image'] : null, ]; } } } return $found; } /** * The plan with one hero changed: its photo, its layout, or both. Null for $image means "automatic". * * @param array $plan * @return array */ public static function update(array $plan, string $sectionId, ?string $image, ?string $variant = null): array { foreach ($plan['pages'] ?? [] as $pageIndex => $page) { foreach ($page['sections'] ?? [] as $sectionIndex => $section) { if (($section['id'] ?? null) === $sectionId && ($section['type'] ?? null) === 'hero') { $plan['pages'][$pageIndex]['sections'][$sectionIndex]['content']['image'] = $image; if ($variant !== null) { $plan['pages'][$pageIndex]['sections'][$sectionIndex]['variant'] = $variant; } } } } return $plan; } /** * Photos the merchant (or the AI) can choose from: uploads first, then one per product. * * @return list */ public static function choices(Store $store): array { $choices = $store->images()->latest('id')->limit(30)->get() ->map(fn (StoreImage $image) => ['ref' => $image->reference(), 'label' => 'Uploaded photo', 'url' => $image->url()]) ->all(); foreach (self::productsWithPhotos($store, 60) as $product) { $choices[] = ['ref' => 'product:'.$product->slug, 'label' => $product->name, 'url' => $product->images->first()->url()]; } return array_values($choices); } /** * References that do not point to a photo of this store. * * @param list $refs * @return list */ public static function unknown(Store $store, array $refs): array { $known = array_column(self::resolve($store, $refs), 'ref'); return array_values(array_diff($refs, $known)); } /** * What the storefront needs: the address of every photo the plan points to, and a few product photos * for heroes without a photo of their own. * * @param array $plan * @return array{refs: object, fallback: list} */ public static function forStorefront(Store $store, array $plan): array { $refs = array_values(array_unique(array_filter(array_column(self::sections($plan), 'image')))); return [ // An object even when empty, so the frontend always gets {} and never []. 'refs' => (object) collect(self::resolve($store, $refs))->mapWithKeys(fn (array $photo) => [$photo['ref'] => $photo['url']])->all(), 'fallback' => array_values(self::productsWithPhotos($store, self::FALLBACK_COUNT) ->map(fn (Product $product) => $product->images->first()->url()) ->all()), ]; } /** * @param list $refs * @return list */ private static function resolve(Store $store, array $refs): array { $uploadIds = []; $slugs = []; foreach ($refs as $ref) { [$kind, $value] = explode(':', $ref, 2) + [1 => '']; if ($kind === 'upload') { $uploadIds[] = (int) $value; } elseif ($kind === 'product') { $slugs[] = $value; } } $found = []; if ($uploadIds !== []) { foreach ($store->images()->whereKey($uploadIds)->get() as $image) { $found[] = ['ref' => $image->reference(), 'url' => $image->url()]; } } if ($slugs !== []) { $products = $store->products()->whereIn('slug', $slugs)->whereHas('images')->with('images')->get(); foreach ($products as $product) { $found[] = ['ref' => 'product:'.$product->slug, 'url' => $product->images->first()->url()]; } } return $found; } /** * Active products that have at least one photo, newest first. * * @return Collection */ private static function productsWithPhotos(Store $store, int $limit): Collection { return $store->products() ->active() ->whereHas('images') ->with('images') ->latest('id') ->limit($limit) ->get() ->filter(fn (Product $product) => $product->images->first() instanceof ProductImage) ->values() ->toBase(); } }