import { useMemo, useState } from 'react' import { p as P } from 'web-components' import type { AdminProductWithRelations } from '@packages/shared' /** * Global products are owned by a superadmin and shared across every region. A * regional admin may only edit the market data for their own region — the * product structure (name, descriptions, images, features, combos, store) is * read-only here. */ interface GlobalProductFormProps { product: AdminProductWithRelations onSubmit: (skus: any[]) => void isLoading: boolean } interface VariantState { price: string marketPrice: string flashPrice: string isFlashAvailable: boolean isOutOfStock: boolean isOffer: boolean isSuspended: boolean } const toNumOrUndef = (v: string): number | undefined => { const n = parseFloat(v) return Number.isFinite(n) ? n : undefined } function GlobalProductForm({ product, onSubmit, isLoading }: GlobalProductFormProps) { const skus = product.skus || [] const [variants, setVariants] = useState( useMemo( () => skus.map((sku) => ({ price: sku.price != null ? String(sku.price) : '', marketPrice: sku.marketPrice != null ? String(sku.marketPrice) : '', flashPrice: sku.flashPrice != null ? String(sku.flashPrice) : '', isFlashAvailable: sku.isFlashAvailable ?? false, isOutOfStock: sku.isOutOfStock ?? false, isOffer: sku.isOffer ?? false, isSuspended: sku.isSuspended ?? false, })), [product] ) ) const setVariant = (index: number, patch: Partial) => setVariants((prev) => prev.map((v, i) => (i === index ? { ...v, ...patch } : v))) const handleSubmit = () => { const payload = skus.map((sku, i) => { const v = variants[i] return { id: sku.id, name: sku.name ?? null, price: toNumOrUndef(v.price) ?? 0, marketPrice: toNumOrUndef(v.marketPrice) ?? null, images: sku.images ?? [], isFlashAvailable: v.isFlashAvailable, flashPrice: v.isFlashAvailable ? (toNumOrUndef(v.flashPrice) ?? null) : null, isOutOfStock: v.isOutOfStock, isOffer: v.isOffer, isComboOnly: sku.isComboOnly ?? false, isDeleted: sku.isDeleted ?? false, isSuspended: v.isSuspended, features: (sku.features || []).map((f) => ({ featureName: f.featureName, featureValue: f.featureValue, })), comboItems: (sku.comboItems || []).map((ci: any) => ({ skuId: ci.skuId })), } }) onSubmit(payload) } const numberInput = (label: string, value: string, onChange: (t: string) => void) => ( ) const toggle = (label: string, checked: boolean, onToggle: () => void) => ( ) return (

Global product

Shared across all regions. You can edit the price & stock for your region only.

Product details (read-only)

{product.name}

{product.shortDescription ?

{product.shortDescription}

: null}

Store: {product.store?.name ?? '—'} · Type: {product.productType}

{skus.map((sku, index) => { const v = variants[index] return (
{sku.images && sku.images[0] ? ( ) : null}

{sku.name || product.name}

{(sku.features || []).map((f) => f.featureValue).join(' · ') || '—'}

Market data (your region)

{numberInput('Price (₹)', v.price, (t) => setVariant(index, { price: t }))} {numberInput('Market price (₹)', v.marketPrice, (t) => setVariant(index, { marketPrice: t }))} {toggle('Flash available', v.isFlashAvailable, () => setVariant(index, { isFlashAvailable: !v.isFlashAvailable }))} {v.isFlashAvailable && numberInput('Flash price (₹)', v.flashPrice, (t) => setVariant(index, { flashPrice: t }))} {toggle('Out of stock', v.isOutOfStock, () => setVariant(index, { isOutOfStock: !v.isOutOfStock }))} {toggle('Offer SKU', v.isOffer, () => setVariant(index, { isOffer: !v.isOffer }))} {toggle('Suspend in my region', v.isSuspended, () => setVariant(index, { isSuspended: !v.isSuspended }))}
) })}
) } export default GlobalProductForm