'use strict';

// src/gallery.ts
function dedupe(urls) {
  const seen = /* @__PURE__ */ new Set();
  const out = [];
  for (const url of urls) {
    if (url && !seen.has(url)) {
      seen.add(url);
      out.push(url);
    }
  }
  return out;
}
function resolveGallery(product, selectedVariantId) {
  const variant = selectedVariantId ? product.variants?.find((v) => v.id === selectedVariantId) : void 0;
  if (variant?.images?.length) return dedupe(variant.images);
  if (product.images?.length) return dedupe(product.images);
  const firstWithImages = product.variants?.find((v) => v.images?.length);
  if (firstWithImages?.images?.length) return dedupe(firstWithImages.images);
  return product.image_url ? [product.image_url] : [];
}
function allGalleryImages(product) {
  const seen = /* @__PURE__ */ new Set();
  const out = [];
  const push = (url, variantId) => {
    if (url && !seen.has(url)) {
      seen.add(url);
      out.push({ url, variantId });
    }
  };
  push(product.image_url, null);
  for (const url of product.images ?? []) push(url, null);
  for (const variant of product.variants ?? []) {
    for (const url of variant.images ?? []) push(url, variant.id);
  }
  return out;
}

// src/utils/variant.ts
function stripProductPrefix(variantName, productName) {
  if (!variantName || !productName) return variantName || productName;
  const lower = variantName.toLowerCase().trim();
  if (lower === "default" || lower === "standard") return productName;
  if (lower.endsWith("-default") || lower.endsWith("-standard")) return productName;
  if (variantName.startsWith(productName)) {
    const rest = variantName.slice(productName.length);
    const match = rest.match(/^\s*[-–—]\s*(.*)/);
    if (match && match[1].length > 0) return match[1];
  }
  const lowerProduct = productName.toLowerCase().trim();
  if (lower.startsWith(lowerProduct)) {
    const rest = variantName.slice(productName.length);
    const match = rest.match(/^\s*[-–—]\s*(.*)/);
    if (match && match[1].length > 0) return match[1];
  }
  const lastDash = variantName.lastIndexOf("-");
  if (lastDash > 0 && lastDash < variantName.length - 1) {
    const prefix = variantName.slice(0, lastDash).trim();
    const suffix = variantName.slice(lastDash + 1).trim();
    if (suffix.length > 0 && prefix.length > suffix.length * 2) {
      return suffix;
    }
  }
  return variantName;
}
function getVariantDisplayName(variant, productName) {
  if (variant.display_attributes?.length) {
    return variant.display_attributes.map((a) => a.value_name).join(" / ");
  }
  if (variant.name) {
    return productName ? stripProductPrefix(variant.name, productName) : variant.name;
  }
  return productName || (variant.is_default ? "Default" : "Option");
}

// src/types/product.ts
var PRODUCT_TYPE = {
  Product: "product",
  Service: "service",
  Digital: "digital",
  Bundle: "bundle",
  Composite: "composite"
};
var RENDER_HINT = {
  Food: "food",
  Physical: "physical",
  General: "general"
};
var DURATION_UNIT = {
  Minutes: "minutes",
  Hours: "hours",
  Days: "days",
  Nights: "nights",
  Weeks: "weeks",
  Months: "months",
  Years: "years"
};
var INPUT_FIELD_TYPE = {
  Text: "text",
  Textarea: "textarea",
  Number: "number",
  Select: "select",
  Radio: "radio",
  Checkbox: "checkbox",
  Color: "color",
  Date: "date",
  File: "file",
  Image: "image",
  Url: "url",
  Address: "address",
  Phone: "phone",
  Email: "email",
  DateTime: "date_time",
  Time: "time",
  Signature: "signature",
  MultiSelect: "multi_select",
  DateRange: "date_range",
  Location: "location"
};

// src/assets/url.ts
var DEFAULT_CDN_BASE_URL = "https://storefrontassetscdn.cimplify.io";
var CIMPLIFY_CDN_HOSTS = [
  "storefrontassetscdn.cimplify.io",
  "cdn.cimplify.io",
  "static-tmp.cimplify.io"
];
var ABSOLUTE_URL_RE = /^https?:\/\//i;
var MOCK_PASSTHROUGH_RE = /^\/(img|elements|_mock)\//;
function assetUrl(path, opts = {}) {
  if (!path) return path;
  if (ABSOLUTE_URL_RE.test(path)) return appendTransform(path, opts);
  if (MOCK_PASSTHROUGH_RE.test(path)) return appendTransform(path, opts);
  const base = (opts.base ?? DEFAULT_CDN_BASE_URL).replace(/\/+$/, "");
  const normalized = path.replace(/^\/+/, "");
  return appendTransform(`${base}/${normalized}`, opts);
}
function isCimplifyAsset(src, base) {
  if (!src) return false;
  if (!ABSOLUTE_URL_RE.test(src)) return true;
  if (MOCK_PASSTHROUGH_RE.test(src)) return true;
  const resolvedBase = (base ?? DEFAULT_CDN_BASE_URL).replace(/\/+$/, "");
  if (resolvedBase && src.startsWith(resolvedBase)) return true;
  return CIMPLIFY_CDN_HOSTS.some((host) => src.startsWith(`https://${host}/`));
}
function appendTransform(url, t) {
  const params = new URLSearchParams();
  if (t.w != null) params.set("w", String(t.w));
  if (t.h != null) params.set("h", String(t.h));
  if (t.quality != null) params.set("quality", String(t.quality));
  if (t.format != null) params.set("format", t.format);
  const query = params.toString();
  if (!query) return url;
  return `${url}${url.includes("?") ? "&" : "?"}${query}`;
}

exports.CIMPLIFY_CDN_HOSTS = CIMPLIFY_CDN_HOSTS;
exports.DEFAULT_CDN_BASE_URL = DEFAULT_CDN_BASE_URL;
exports.DURATION_UNIT = DURATION_UNIT;
exports.INPUT_FIELD_TYPE = INPUT_FIELD_TYPE;
exports.PRODUCT_TYPE = PRODUCT_TYPE;
exports.RENDER_HINT = RENDER_HINT;
exports.allGalleryImages = allGalleryImages;
exports.assetUrl = assetUrl;
exports.getVariantDisplayName = getVariantDisplayName;
exports.isCimplifyAsset = isCimplifyAsset;
exports.resolveGallery = resolveGallery;
