var _excluded = ["orderNote"],
  _excluded2 = ["orderNote"],
  _excluded3 = ["shopDiscount"],
  _excluded4 = ["contactsInfo"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
import { Collapse } from 'antd';

/**
 * 高级配置面板：fixture override 表单。
 *
 * 三种 variant 字段集合：
 *  - detail: orderId
 *  - create: loadProductsParams.{schedule_date / menu_list_ids / schedule_datetime}, customerId, productPicker
 *  - edit:   orderId + create 的所有字段
 *
 * 注意：本面板只维护「用户填了什么」（Partial override），由父组件负责与 fixture base 合并。
 */

function parseNumberOrUndefined(raw) {
  var t = raw.trim();
  if (!t) return undefined;
  var n = Number(t);
  return Number.isFinite(n) ? n : undefined;
}
function parseMenuListIds(raw) {
  var t = raw.trim();
  if (!t) return undefined;
  return t.split(/[,\s]+/).map(function (s) {
    return Number(s);
  }).filter(function (n) {
    return Number.isFinite(n);
  });
}
function FieldText(_ref) {
  var label = _ref.label,
    value = _ref.value,
    placeholder = _ref.placeholder,
    _onChange = _ref.onChange;
  return /*#__PURE__*/React.createElement("label", {
    className: "sales-sdk-demo__field"
  }, /*#__PURE__*/React.createElement("span", null, label), /*#__PURE__*/React.createElement("input", {
    value: value,
    placeholder: placeholder,
    onChange: function onChange(e) {
      return _onChange(e.target.value);
    }
  }));
}
function DetailForm(_ref2) {
  var value = _ref2.value,
    _onChange2 = _ref2.onChange;
  return /*#__PURE__*/React.createElement("div", {
    className: "sales-sdk-demo__advanced-form"
  }, /*#__PURE__*/React.createElement(FieldText, {
    label: "orderId",
    value: value.orderId != null ? String(value.orderId) : '',
    placeholder: "\u4F7F\u7528 fixture \u9ED8\u8BA4\u503C",
    onChange: function onChange(next) {
      return _onChange2(_objectSpread(_objectSpread({}, value), {}, {
        orderId: parseNumberOrUndefined(next)
      }));
    }
  }));
}
function MutableForm(_ref3) {
  var _params$schedule_date, _params$schedule_date2;
  var value = _ref3.value,
    _onChange3 = _ref3.onChange;
  var params = value.loadProductsParams || {};
  var picker = value.productPicker || {};
  var setParams = function setParams(patch) {
    _onChange3(_objectSpread(_objectSpread({}, value), {}, {
      loadProductsParams: _objectSpread(_objectSpread({}, params), patch)
    }));
  };
  var setPicker = function setPicker(patch) {
    _onChange3(_objectSpread(_objectSpread({}, value), {}, {
      productPicker: _objectSpread(_objectSpread({}, picker), patch)
    }));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "sales-sdk-demo__advanced-form"
  }, /*#__PURE__*/React.createElement(FieldText, {
    label: "loadProductsParams.schedule_date",
    value: (_params$schedule_date = params.schedule_date) !== null && _params$schedule_date !== void 0 ? _params$schedule_date : '',
    placeholder: "YYYY-MM-DD",
    onChange: function onChange(next) {
      return setParams({
        schedule_date: next || undefined
      });
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "loadProductsParams.schedule_datetime",
    value: (_params$schedule_date2 = params.schedule_datetime) !== null && _params$schedule_date2 !== void 0 ? _params$schedule_date2 : '',
    placeholder: "YYYY-MM-DD HH:mm:ss",
    onChange: function onChange(next) {
      return setParams({
        schedule_datetime: next || undefined
      });
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "loadProductsParams.menu_list_ids",
    value: Array.isArray(params.menu_list_ids) ? params.menu_list_ids.join(',') : '',
    placeholder: "\u9017\u53F7\u5206\u9694\uFF0C\u5982 44858,44859",
    onChange: function onChange(next) {
      return setParams({
        menu_list_ids: parseMenuListIds(next)
      });
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "customerId",
    value: value.customerId != null ? String(value.customerId) : '',
    placeholder: "\u7F3A\u7701 = customer.list[0]",
    onChange: function onChange(next) {
      var parsed = parseNumberOrUndefined(next);
      _onChange3(_objectSpread(_objectSpread({}, value), {}, {
        customerId: parsed
      }));
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "productPicker.productId",
    value: picker.productId != null ? String(picker.productId) : '',
    placeholder: "\u7F3A\u7701 = products[0].id",
    onChange: function onChange(next) {
      return setPicker({
        productId: parseNumberOrUndefined(next)
      });
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "productPicker.variantIndex",
    value: picker.variantIndex != null ? String(picker.variantIndex) : '',
    placeholder: "\u7F3A\u7701 0",
    onChange: function onChange(next) {
      return setPicker({
        variantIndex: parseNumberOrUndefined(next)
      });
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "productPicker.num",
    value: picker.num != null ? String(picker.num) : '',
    placeholder: "\u7F3A\u7701 1",
    onChange: function onChange(next) {
      return setPicker({
        num: parseNumberOrUndefined(next)
      });
    }
  }));
}
function OrderMetaForm(_ref4) {
  var _value$orderNote;
  var value = _ref4.value,
    _onChange4 = _ref4.onChange;
  var contactsJson = value.contactsInfo === undefined ? '' : value.contactsInfo === null ? 'null' : JSON.stringify(value.contactsInfo);
  return /*#__PURE__*/React.createElement("div", {
    className: "sales-sdk-demo__advanced-form sales-sdk-demo__advanced-form--meta"
  }, /*#__PURE__*/React.createElement(FieldText, {
    label: "orderNote\uFF08sales.setOrderNote\uFF09",
    value: (_value$orderNote = value.orderNote) !== null && _value$orderNote !== void 0 ? _value$orderNote : '',
    placeholder: "\u7559\u7A7A = \u4F7F\u7528 fixture \u9ED8\u8BA4\uFF1B\u586B __skip__ \u8DF3\u8FC7\u6B65\u9AA4",
    onChange: function onChange(next) {
      var t = next.trim();
      if (t === '__skip__') {
        var _omit = value.orderNote,
          rest = _objectWithoutProperties(value, _excluded);
        _onChange4(rest);
        return;
      }
      if (!t) {
        var _omit2 = value.orderNote,
          _rest = _objectWithoutProperties(value, _excluded2);
        _onChange4(_rest);
        return;
      }
      _onChange4(_objectSpread(_objectSpread({}, value), {}, {
        orderNote: t
      }));
    }
  }), /*#__PURE__*/React.createElement(FieldText, {
    label: "shopDiscount\uFF08sales.setShopDiscount\uFF09",
    value: value.shopDiscount != null ? String(value.shopDiscount) : '',
    placeholder: "\u5982 5 \u6216 5.00\uFF1B\u586B __skip__ \u8DF3\u8FC7\u6B65\u9AA4",
    onChange: function onChange(next) {
      var t = next.trim();
      if (t === '__skip__' || !t) {
        var _omit = value.shopDiscount,
          rest = _objectWithoutProperties(value, _excluded3);
        _onChange4(rest);
        return;
      }
      var n = Number(t);
      _onChange4(_objectSpread(_objectSpread({}, value), {}, {
        shopDiscount: Number.isFinite(n) ? n : t
      }));
    }
  }), /*#__PURE__*/React.createElement("label", {
    className: "sales-sdk-demo__field"
  }, /*#__PURE__*/React.createElement("span", null, "contactsInfo\uFF08sales.setContactsInfo\uFF0CJSON\uFF09"), /*#__PURE__*/React.createElement("textarea", {
    className: "sales-sdk-demo__textarea",
    value: contactsJson,
    placeholder: "\u5982 {\"name\":\"Demo\"}\uFF1B\u586B __skip__ \u8DF3\u8FC7\uFF1B\u586B null \u6E05\u7A7A",
    rows: 3,
    onChange: function onChange(e) {
      var t = e.target.value.trim();
      if (t === '__skip__' || t === '') {
        var _omit = value.contactsInfo,
          rest = _objectWithoutProperties(value, _excluded4);
        _onChange4(rest);
        return;
      }
      if (t === 'null') {
        _onChange4(_objectSpread(_objectSpread({}, value), {}, {
          contactsInfo: null
        }));
        return;
      }
      try {
        var parsed = JSON.parse(t);
        if (parsed === null || _typeof(parsed) !== 'object' || Array.isArray(parsed)) {
          throw new Error('contactsInfo 须为 JSON 对象或 null');
        }
        _onChange4(_objectSpread(_objectSpread({}, value), {}, {
          contactsInfo: parsed
        }));
      } catch (_unused) {
        /* 输入中途非法 JSON 时不写回，避免打断编辑 */
      }
    }
  })));
}
function EditForm(_ref5) {
  var value = _ref5.value,
    _onChange5 = _ref5.onChange;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(FieldText, {
    label: "orderId",
    value: value.orderId != null ? String(value.orderId) : '',
    placeholder: "\u4F7F\u7528 fixture \u9ED8\u8BA4\u503C",
    onChange: function onChange(next) {
      return _onChange5(_objectSpread(_objectSpread({}, value), {}, {
        orderId: parseNumberOrUndefined(next)
      }));
    }
  }), /*#__PURE__*/React.createElement(OrderMetaForm, {
    value: value,
    onChange: _onChange5
  }), /*#__PURE__*/React.createElement(MutableForm, {
    value: value,
    onChange: _onChange5
  }));
}

/**
 * 顶层组件，按 variant 分发渲染对应字段；默认收起以保留「一键」干净视觉。
 */
export var DemoAdvancedConfig = function DemoAdvancedConfig(props) {
  var renderBody = function renderBody() {
    if (props.variant === 'detail') {
      return /*#__PURE__*/React.createElement(DetailForm, {
        value: props.value,
        onChange: props.onChange
      });
    }
    if (props.variant === 'create') {
      return /*#__PURE__*/React.createElement(MutableForm, {
        value: props.value,
        onChange: props.onChange
      });
    }
    return /*#__PURE__*/React.createElement(EditForm, {
      value: props.value,
      onChange: props.onChange
    });
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "sales-sdk-demo__advanced"
  }, /*#__PURE__*/React.createElement(Collapse, {
    ghost: true,
    items: [{
      key: 'adv',
      label: '高级配置（覆盖 fixture）',
      children: renderBody()
    }]
  }));
};