import { inject, Injectable, InjectionToken, Provider } from '@angular/core';
import { Config } from '@isa/core/config';
import { z } from 'zod';

export const PRODUCT_IMAGE_URL = new InjectionToken<string>('PRODUCT_IMAGE_URL', {
  factory: () => inject(Config).get('@cdn/product-image.url', z.string().url()),
});

@Injectable({ providedIn: 'root' })
export class ProductImageService {
  readonly imageUrl = inject(PRODUCT_IMAGE_URL);

  getImageUrl({
    imageId,
    width = 150,
    height = 150,
    showDummy = true,
  }: {
    imageId: string;
    width?: number;
    height?: number;
    showDummy?: boolean;
  }): string {
    return `${this.imageUrl}/${imageId}_${width}x${height}.jpg?showDummy=${showDummy}`;
  }
}

export function provideProductImageUrl(url: string): Provider[] {
  return [
    ProductImageService,
    {
      provide: PRODUCT_IMAGE_URL,
      useValue: url,
    },
  ];
}
