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 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, { useMemo, useEffect } from "react";
import { Form, Skeleton } from "antd";
import { request } from "../../utils/index";
import useEngineContext from "../../hooks/useEngineContext";
import { getProductExtensionFields } from "./server";
import { useRequest } from "ahooks";
import { fields } from "./fields";
import Card from "./card";
import "./index.less";
import { locales } from "@pisell/utils";
import localeTexts from "./locales";
import { getLocale } from "../../utils/locales";
locales.init(localeTexts, getLocale());
var ProductExtension = function ProductExtension(_ref) {
  var _extension$filed;
  var onDataChange = _ref.onDataChange,
    type = _ref.type;
  var context = useEngineContext();
  request.setRequest(context.appHelper.utils.request);
  var _getProductExtensionFields = useRequest(getProductExtensionFields, {
    manual: true,
    onSuccess: function onSuccess(res) {
      onDataChange === null || onDataChange === void 0 || onDataChange(res);
    }
  });
  useEffect(function () {
    _getProductExtensionFields.run({
      code: "event_item"
    });
  }, []);
  var extension = useMemo(function () {
    return _getProductExtensionFields.data || {};
  }, [_getProductExtensionFields.data]);
  var renderItem = function renderItem(item, name) {
    var _ref2 = fields[item.key] || {},
      Component = _ref2.Component,
      defaultProps = _ref2.defaultProps;
    var _props = _objectSpread({}, defaultProps);
    if (name) {
      _props.name = name;
    }
    return Component ? /*#__PURE__*/React.createElement(Component, _props) : null;
  };
  return /*#__PURE__*/React.createElement(Skeleton, {
    active: true,
    round: true,
    loading: _getProductExtensionFields.loading
  }, (_extension$filed = extension.filed) === null || _extension$filed === void 0 ? void 0 : _extension$filed.map(function (item, index) {
    var _item$, _item$2, _item$3, _extension$extension;
    var isHidden = ((_item$ = item[0]) === null || _item$ === void 0 ? void 0 : _item$.key) === "event_item_type";
    // 编辑单个活动, 不展示schedule组件
    if (type !== "all" && ((_item$2 = item[0]) === null || _item$2 === void 0 ? void 0 : _item$2.key) === "schedule.ids") {
      return null;
    }
    return /*#__PURE__*/React.createElement(Card, {
      hidden: isHidden,
      key: index
      // style={{ overflow: "inherit" }}
      ,
      header: item.length === 1 ? (_item$3 = item[0]) === null || _item$3 === void 0 ? void 0 : _item$3.name : extension === null || extension === void 0 || (_extension$extension = extension.extension) === null || _extension$extension === void 0 ? void 0 : _extension$extension.name
    }, item.map(function (child, childIndex) {
      var name = "".concat(child.id, "|").concat(child.key);
      return /*#__PURE__*/React.createElement(Form.Item, {
        hidden: isHidden,
        label: item.length === 1 ? "" : child.name,
        name: name,
        rules: child.rules || child.key === "par_value" ? [{
          required: true,
          message: locales.getText("pisell2.text.product-extension.please-select")
        }] : [],
        key: childIndex,
        noStyle: true,
        initialValue: child.key === "event_item_capacity" ? 1 : undefined
      }, renderItem(child, name));
    }));
  }));
};
export default ProductExtension;