import {
  optionalFloat,
  optionalInt
} from "./chunk-UXD3VNBO.js";
import {
  castNumber
} from "./chunk-SMQT3ZNO.js";
import {
  i18n
} from "./chunk-ZMDE5HEV.js";

// src/pages/products/product-create/constants.ts
import { z } from "zod";

// src/pages/products/product-create/utils.ts
var normalizeProductFormValues = (values) => {
  const thumbnail = values.media?.find((media) => media.isThumbnail)?.url;
  const images = values.media?.filter((media) => !media.isThumbnail).map((media) => ({ url: media.url }));
  return {
    status: values.status,
    is_giftcard: false,
    ...values.seller_id ? { additional_data: { seller_id: values.seller_id } } : {},
    tags: values?.tags?.length ? values.tags?.map((tag) => ({ id: tag })) : void 0,
    sales_channels: values?.sales_channels?.length ? values.sales_channels?.map((sc) => ({ id: sc.id })) : void 0,
    images,
    collection_id: values.collection_id || void 0,
    shipping_profile_id: values.shipping_profile_id || void 0,
    categories: values.categories.map((id) => ({ id })),
    type_id: values.type_id || void 0,
    handle: values.handle?.trim(),
    origin_country: values.origin_country || void 0,
    material: values.material || void 0,
    mid_code: values.mid_code || void 0,
    hs_code: values.hs_code || void 0,
    thumbnail,
    title: values.title.trim(),
    subtitle: values.subtitle?.trim(),
    description: values.description?.trim(),
    discountable: values.discountable,
    width: values.width ? parseFloat(values.width) : void 0,
    length: values.length ? parseFloat(values.length) : void 0,
    height: values.height ? parseFloat(values.height) : void 0,
    weight: values.weight ? parseFloat(values.weight) : void 0,
    options: values.options.filter((o) => o.title),
    // clean temp. values
    variants: normalizeVariants(
      values.variants.filter((variant) => variant.should_create),
      values.regionsCurrencyMap
    )
  };
};
var normalizeVariants = (variants, regionsCurrencyMap) => {
  return variants.map((variant) => ({
    title: variant.title || Object.values(variant.options || {}).join(" / "),
    options: variant.options,
    sku: variant.sku || void 0,
    manage_inventory: !!variant.manage_inventory,
    allow_backorder: !!variant.allow_backorder,
    variant_rank: variant.variant_rank,
    inventory_items: variant.inventory.map((i) => {
      const quantity = i.required_quantity ? castNumber(i.required_quantity) : null;
      if (!i.inventory_item_id || !quantity) {
        return false;
      }
      return {
        ...i,
        required_quantity: quantity
      };
    }).filter(
      (item) => item !== false
    ),
    prices: Object.entries(variant.prices || {}).map(([key, value]) => {
      if (value === "" || value === void 0) {
        return void 0;
      }
      if (key.startsWith("reg_")) {
        return {
          currency_code: regionsCurrencyMap[key],
          amount: castNumber(value),
          rules: { region_id: key }
        };
      } else {
        return {
          currency_code: key,
          amount: castNumber(value)
        };
      }
    }).filter((v) => !!v)
  }));
};
var decorateVariantsWithDefaultValues = (variants) => {
  return variants.map((variant) => ({
    ...variant,
    title: variant.title || "",
    sku: variant.sku || "",
    manage_inventory: variant.manage_inventory || false,
    allow_backorder: variant.allow_backorder || false,
    inventory_kit: variant.inventory_kit || false
  }));
};

// src/pages/products/product-create/constants.ts
var MediaSchema = z.object({
  id: z.string().optional(),
  url: z.string(),
  isThumbnail: z.boolean(),
  file: z.any().nullable()
  // File
});
var ProductCreateVariantSchema = z.object({
  should_create: z.boolean(),
  is_default: z.boolean().optional(),
  title: z.string(),
  upc: z.string().optional(),
  ean: z.string().optional(),
  barcode: z.string().optional(),
  mid_code: z.string().optional(),
  hs_code: z.string().optional(),
  width: optionalInt,
  height: optionalInt,
  length: optionalInt,
  weight: optionalInt,
  material: z.string().optional(),
  origin_country: z.string().optional(),
  sku: z.string().optional(),
  manage_inventory: z.boolean().optional(),
  allow_backorder: z.boolean().optional(),
  inventory_kit: z.boolean().optional(),
  options: z.record(z.string(), z.string()),
  variant_rank: z.number(),
  prices: z.record(z.string(), optionalFloat).optional(),
  inventory: z.array(
    z.object({
      inventory_item_id: z.string(),
      required_quantity: optionalInt
    })
  ).optional()
});
var ProductCreateOptionSchema = z.object({
  title: z.string(),
  values: z.array(z.string()).min(1)
});
var ProductCreateSchema = z.object({
  title: z.string().min(1),
  subtitle: z.string().optional(),
  handle: z.string().optional(),
  description: z.string().optional(),
  discountable: z.boolean(),
  type_id: z.string().optional(),
  collection_id: z.string().optional(),
  shipping_profile_id: z.string().optional(),
  categories: z.array(z.string()),
  tags: z.array(z.string()).optional(),
  seller_id: z.string().optional(),
  sales_channels: z.array(
    z.object({
      id: z.string(),
      name: z.string()
    })
  ).optional(),
  origin_country: z.string().optional(),
  material: z.string().optional(),
  width: z.string().optional(),
  length: z.string().optional(),
  height: z.string().optional(),
  weight: z.string().optional(),
  mid_code: z.string().optional(),
  hs_code: z.string().optional(),
  options: z.array(ProductCreateOptionSchema).min(1),
  enable_variants: z.boolean(),
  variants: z.array(ProductCreateVariantSchema).min(1),
  media: z.array(MediaSchema).optional()
}).superRefine((data, ctx) => {
  if (data.variants.every((v) => !v.should_create)) {
    return ctx.addIssue({
      code: z.ZodIssueCode.custom,
      path: ["variants"],
      message: "invalid_length"
    });
  }
  const skus = /* @__PURE__ */ new Set();
  data.variants.forEach((v, index) => {
    if (v.sku) {
      if (skus.has(v.sku)) {
        ctx.addIssue({
          code: z.ZodIssueCode.custom,
          path: [`variants.${index}.sku`],
          message: i18n.t("products.create.errors.uniqueSku")
        });
      }
      skus.add(v.sku);
    }
  });
});
var EditProductMediaSchema = z.object({
  media: z.array(MediaSchema)
});
var PRODUCT_CREATE_FORM_DEFAULTS = {
  discountable: true,
  tags: [],
  sales_channels: [],
  options: [
    {
      title: "Default option",
      values: ["Default option value"]
    }
  ],
  variants: decorateVariantsWithDefaultValues([
    {
      title: "Default variant",
      should_create: true,
      variant_rank: 0,
      options: {
        "Default option": "Default option value"
      },
      inventory: [{ inventory_item_id: "", required_quantity: "" }],
      is_default: true
    }
  ]),
  enable_variants: false,
  media: [],
  categories: [],
  seller_id: "",
  collection_id: "",
  shipping_profile_id: "",
  description: "",
  handle: "",
  height: "",
  hs_code: "",
  length: "",
  material: "",
  mid_code: "",
  origin_country: "",
  subtitle: "",
  title: "",
  type_id: "",
  weight: "",
  width: ""
};

export {
  normalizeProductFormValues,
  decorateVariantsWithDefaultValues,
  MediaSchema,
  ProductCreateSchema,
  EditProductMediaSchema,
  PRODUCT_CREATE_FORM_DEFAULTS
};
