import { locales } from '@pisell/utils';
function parseUnitPrice(input) {
  var _input$priceValue;
  var raw = Number.parseFloat(String((_input$priceValue = input.priceValue) !== null && _input$priceValue !== void 0 ? _input$priceValue : '').trim());
  var abs = Number.isFinite(raw) ? Math.abs(raw) : 0;
  var signed = input.priceType === 'deduction' ? -abs : abs;
  return signed.toFixed(2);
}

/**
 * 将「创建并添加一次性商品」弹窗数据转为 OS `addProductToOrder` 入参。
 * 自定义商品写入 tempOrder.products，`product_id` 为 null；折扣开关走 metadata.discountable_flag。
 */
export function buildCustomOrderProductFromCustomiseItem(input) {
  var _input$productName;
  var productName = ((_input$productName = input.productName) === null || _input$productName === void 0 ? void 0 : _input$productName.trim()) || locales.getText('pisell2.customise-item.default-product-name');
  var unitPrice = parseUnitPrice(input);
  var quantity = Math.max(1, Math.floor(Number(input.quantity) || 1));
  return {
    product_id: null,
    product_variant_id: 0,
    order_detail_id: null,
    num: quantity,
    selling_price: unitPrice,
    original_price: unitPrice,
    tax_fee: '0.00',
    is_charge_tax: 0,
    discount_list: [],
    product_option_item: [],
    product_bundle: [],
    note: '',
    metadata: {
      discountable_flag: input.vouchersApplicable ? 1 : 0,
      origin: {
        title: productName,
        name: productName,
        isCustomItem: true
      }
    },
    _origin: {
      title: productName,
      name: productName,
      isCustomItem: true,
      price: unitPrice
    }
  };
}