{"version":3,"file":"ProductPrice.mjs","names":[],"sources":["../../src/ProductPrice.tsx"],"sourcesContent":["import type {\n  MoneyV2,\n  UnitPriceMeasurement,\n  Product,\n} from './storefront-api-types.js';\nimport {Money, type MoneyProps, type MoneyPropsBase} from './Money.js';\nimport type {PartialDeep} from 'type-fest';\nimport {flattenConnection} from './flatten-connection.js';\n\nexport interface ProductPriceProps {\n  /** A Storefront API [Product object](https://shopify.dev/api/storefront/reference/products/product). */\n  data: PartialDeep<Product, {recurseIntoArrays: true}>;\n  /** The type of price. Valid values: `regular` (default) or `compareAt`. */\n  priceType?: 'regular' | 'compareAt';\n  /** The type of value. Valid values: `min` (default), `max` or `unit`. */\n  valueType?: 'max' | 'min' | 'unit';\n  /** The ID of the variant. */\n  variantId?: string;\n}\n\n/**\n * The `ProductPrice` component renders a `Money` component with the product\n * [`priceRange`](https://shopify.dev/api/storefront/reference/products/productpricerange)'s `maxVariantPrice` or `minVariantPrice`, for either the regular price or compare at price range.\n * @publicDocs\n */\nexport function ProductPrice<\n  ComponentGeneric extends React.ElementType = 'div',\n>(\n  props: ProductPriceProps &\n    Omit<MoneyProps<ComponentGeneric>, 'data' | 'measurement'>,\n): JSX.Element | null {\n  const {\n    priceType = 'regular',\n    variantId,\n    valueType = 'min',\n    data: product,\n    ...passthroughProps\n  } = props;\n\n  if (product == null) {\n    throw new Error(`<ProductPrice/> requires a product as the 'data' prop`);\n  }\n\n  let price: Partial<MoneyV2> | undefined | null;\n  let measurement: Partial<UnitPriceMeasurement> | undefined | null;\n\n  const variant = variantId\n    ? (flattenConnection(product?.variants ?? {}).find(\n        (variant) => variant?.id === variantId,\n      ) ?? null)\n    : null;\n\n  const variantPriceProperty =\n    valueType === 'max' ? 'maxVariantPrice' : 'minVariantPrice';\n\n  if (priceType === 'compareAt') {\n    if (variantId && variant) {\n      price = variant.compareAtPrice;\n    } else {\n      price = product?.compareAtPriceRange?.[variantPriceProperty];\n    }\n\n    let priceAsNumber: number;\n    if (variantId && variant) {\n      priceAsNumber = parseFloat(variant.price?.amount ?? '0');\n    } else {\n      priceAsNumber = parseFloat(\n        product?.priceRange?.[variantPriceProperty]?.amount ?? '0',\n      );\n    }\n\n    const compareAtPriceAsNumber = parseFloat(price?.amount ?? '0');\n\n    if (priceAsNumber >= compareAtPriceAsNumber) {\n      return null;\n    }\n  } else {\n    if (variantId && variant) {\n      price = variant.price;\n      if (valueType === 'unit') {\n        price = variant.unitPrice;\n        measurement = variant.unitPriceMeasurement;\n      }\n    } else if (valueType === 'max') {\n      price = product.priceRange?.maxVariantPrice;\n    } else {\n      price = product.priceRange?.minVariantPrice;\n    }\n  }\n\n  if (!price) {\n    return null;\n  }\n\n  if (measurement) {\n    return (\n      <Money {...passthroughProps} data={price} measurement={measurement} />\n    );\n  }\n\n  return <Money {...passthroughProps} data={price} />;\n}\n\n// This is only for documentation purposes, and it is not used in the code.\n/** @publicDocs */\nexport interface ProductPricePropsForDocs<\n  AsType extends React.ElementType = 'div',\n>\n  extends\n    Omit<MoneyPropsBase<AsType>, 'data' | 'measurement'>,\n    ProductPriceProps {}\n"],"mappings":";;;;;;;;;AAyBA,SAAgB,aAGd,OAEoB;CACpB,MAAM,EACJ,YAAY,WACZ,WACA,YAAY,OACZ,MAAM,SACN,GAAG,qBACD;AAEJ,KAAI,WAAW,KACb,OAAM,IAAI,MAAM,wDAAwD;CAG1E,IAAI;CACJ,IAAI;CAEJ,MAAM,UAAU,YACX,kBAAkB,SAAS,YAAY,EAAE,CAAC,CAAC,MACzC,YAAY,SAAS,OAAO,UAC9B,IAAI,OACL;CAEJ,MAAM,uBACJ,cAAc,QAAQ,oBAAoB;AAE5C,KAAI,cAAc,aAAa;AAC7B,MAAI,aAAa,QACf,SAAQ,QAAQ;MAEhB,SAAQ,SAAS,sBAAsB;EAGzC,IAAI;AACJ,MAAI,aAAa,QACf,iBAAgB,WAAW,QAAQ,OAAO,UAAU,IAAI;MAExD,iBAAgB,WACd,SAAS,aAAa,uBAAuB,UAAU,IACxD;EAGH,MAAM,yBAAyB,WAAW,OAAO,UAAU,IAAI;AAE/D,MAAI,iBAAiB,uBACnB,QAAO;YAGL,aAAa,SAAS;AACxB,UAAQ,QAAQ;AAChB,MAAI,cAAc,QAAQ;AACxB,WAAQ,QAAQ;AAChB,iBAAc,QAAQ;;YAEf,cAAc,MACvB,SAAQ,QAAQ,YAAY;KAE5B,SAAQ,QAAQ,YAAY;AAIhC,KAAI,CAAC,MACH,QAAO;AAGT,KAAI,YACF,QACE,oBAAC,OAAD;EAAO,GAAI;EAAkB,MAAM;EAAoB;EAAe,CAAA;AAI1E,QAAO,oBAAC,OAAD;EAAO,GAAI;EAAkB,MAAM;EAAS,CAAA"}