var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/ruleSetting/index.tsx
var ruleSetting_exports = {};
__export(ruleSetting_exports, {
  default: () => ruleSetting_default
});
module.exports = __toCommonJS(ruleSetting_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_classnames = __toESM(require("classnames"));
var import_listComponent = __toESM(require("../listComponent"));
var import_utils = require("@pisell/utils");
var import_useEngineContext = __toESM(require("../../hooks/useEngineContext"));
var import_index = require("./index.less");
var import_ruleBorder = __toESM(require("./ruleBorder"));
var import_utils2 = require("../../utils/index");
var import_utils3 = require("@pisell/utils");
var import_server = require("./server");
var import_locales = __toESM(require("./locales"));
var import_locales2 = require("../../utils/locales");
var createRuleItem = () => {
  return {
    operator: "contain",
    class: "type",
    value: ["all"],
    _id: (0, import_utils.getUniqueId)()
  };
};
import_utils3.locales.init(import_locales.default, (0, import_locales2.getLocale)());
var RuleSetting = (props) => {
  var _a, _b;
  const {
    value = {
      rule: [],
      type: "and"
    },
    onChange,
    className,
    style
  } = props;
  console.log("valuevalue", value);
  const context = (0, import_useEngineContext.default)();
  if ((_b = (_a = context == null ? void 0 : context.appHelper) == null ? void 0 : _a.utils) == null ? void 0 : _b.request) {
    import_utils2.request.setRequest(context.appHelper.utils.request);
  }
  const listRef = (0, import_react.useRef)(null);
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [orderTypes, setOrderTypes] = (0, import_react.useState)([]);
  const [salesChannels, setSalesChannels] = (0, import_react.useState)([]);
  const [staffs, setStaffs] = (0, import_react.useState)([]);
  const [paymentMethods, setPaymentMethod] = (0, import_react.useState)([]);
  const init = async () => {
    let _getStaffList = (0, import_server.getStaffList)();
    const _getProductChannel = (0, import_server.getProductChannel)();
    const _getOrderTypes = (0, import_server.getOrderTypes)();
    const _getPaymentTypes = (0, import_server.getPaymentTypes)();
    const [staffs2, clannels, orders, payments] = await Promise.all([
      _getStaffList,
      _getProductChannel,
      _getOrderTypes,
      _getPaymentTypes
    ]);
    setStaffs(staffs2);
    setSalesChannels(clannels);
    setOrderTypes(orders);
    setPaymentMethod(payments);
  };
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
    init();
  }, []);
  const config = (0, import_react.useMemo)(() => {
    return {
      type: {
        key: "type",
        style: { width: 200 },
        size: "large",
        options: [
          {
            label: import_utils3.locales.getText("pisell2.text.rule-setting.order_type"),
            value: "type",
            list: orderTypes
          },
          {
            label: import_utils3.locales.getText("pisell2.text.rule-setting.sales_channel"),
            value: "platform",
            list: salesChannels
          },
          {
            label: import_utils3.locales.getText("pisell2.text.rule-setting.staff"),
            value: "staff",
            list: staffs
          },
          {
            label: import_utils3.locales.getText("pisell2.text.rule-setting.payment_method"),
            value: "payment_method",
            list: paymentMethods
          },
          {
            label: import_utils3.locales.getText(
              "pisell2.text.rule-setting.pisell_terminal_device"
            ),
            value: "pos",
            list: [
              {
                label: import_utils3.locales.getText(
                  "pisell2.text.rule-setting.current_device"
                ),
                value: "current"
              }
            ]
          }
        ]
      },
      rule: {
        key: "rule",
        style: { width: 100 },
        size: "large",
        options: [
          {
            label: import_utils3.locales.getText("pisell2.text.rule-setting.Contain"),
            value: "contain"
          },
          {
            label: import_utils3.locales.getText("pisell2.text.rule-setting.Exclude"),
            value: "exclude"
          }
        ]
      },
      options: {
        key: "options",
        style: { flex: 1 },
        size: "large",
        mode: "multiple",
        filterOption: false,
        showSearch: false
      }
    };
  }, [staffs, salesChannels, orderTypes, paymentMethods]);
  const handleAddItem = (item) => {
    let _item = { ...item };
    _item.rule.push(createRuleItem());
    return _item;
  };
  const updateRuleItem = (item, cId, val) => {
    let _item = {
      ...item,
      rule: item.rule.map((d) => {
        if (d._id === cId) {
          return {
            ...d,
            ...val
          };
        }
        return d;
      })
    };
    return _item;
  };
  const filterAllOption = (list, current) => {
    let _list = [...list || []];
    if (!_list.includes("all")) {
      return _list;
    }
    if (_list.length === 1) {
      return _list;
    }
    if (!(current || []).includes("all")) {
      return ["all"];
    }
    return _list.filter((d) => d !== "all");
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Skeleton,
    {
      round: true,
      active: true,
      loading,
      className,
      style
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "rule-setting-wrap" }, value.rule.length > 1 ? /* @__PURE__ */ import_react.default.createElement(
      import_ruleBorder.default,
      {
        value: value.type,
        onChange: (e) => {
          onChange({ ...value, type: e });
        },
        startDomClassName: `rule-setting-item-${value.rule[0]._id}`,
        endDomClassName: `rule-setting-item-${value.rule[value.rule.length - 1]._id}`
      }
    ) : null, /* @__PURE__ */ import_react.default.createElement(
      import_listComponent.default,
      {
        ref: listRef,
        value: value.rule,
        onChange: (rule) => {
          onChange({ ...value, rule });
        },
        rowKey: "_id",
        wrapClassName: "rule-setting-list",
        createItem: (item) => {
          return {
            ...item,
            type: "and",
            rule: [
              {
                _id: "-1",
                class: "type",
                operator: "contain",
                value: ["all"]
              }
            ]
          };
        },
        renderItem: (item, index, remove, update) => {
          return /* @__PURE__ */ import_react.default.createElement(
            "div",
            {
              className: (0, import_classnames.default)(
                "rule-setting-item",
                `rule-setting-item-${item._id}`
              ),
              key: item._id
            },
            /* @__PURE__ */ import_react.default.createElement("div", { className: "rule-setting-item-header" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "rule-setting-item-title" }, import_utils3.locales.getText("pisell2.text.satisfy_any_condition")), /* @__PURE__ */ import_react.default.createElement("div", { className: "rule-setting-item-btns" }, /* @__PURE__ */ import_react.default.createElement(
              import_icons.PlusOutlined,
              {
                className: "rule-setting-item-btn",
                onClick: () => {
                  update(item, handleAddItem(item));
                }
              }
            ), value.rule.length > 1 ? /* @__PURE__ */ import_react.default.createElement(
              import_icons.CloseOutlined,
              {
                className: "rule-setting-item-btn",
                onClick: () => {
                  remove(item);
                }
              }
            ) : null)),
            /* @__PURE__ */ import_react.default.createElement("div", { className: "rule-setting-item-body" }, item.rule.length > 1 ? /* @__PURE__ */ import_react.default.createElement(
              import_ruleBorder.default,
              {
                value: item.type,
                onChange: (e) => {
                  update(item, { ...item, type: e });
                },
                startDomClassName: `rule-setting-item-body-item-${item.rule[0]._id}`,
                endDomClassName: `rule-setting-item-body-item-${item.rule[item.rule.length - 1]._id}`
              }
            ) : null, /* @__PURE__ */ import_react.default.createElement("div", { className: "rule-setting-item-body-list" }, item.rule.map((rule) => {
              var _a2;
              return /* @__PURE__ */ import_react.default.createElement(
                "div",
                {
                  className: (0, import_classnames.default)(
                    "rule-setting-item-body-item",
                    `rule-setting-item-body-item-${rule._id}`
                  ),
                  key: rule._id
                },
                /* @__PURE__ */ import_react.default.createElement(
                  import_antd.Select,
                  {
                    ...config.type,
                    value: rule.class,
                    onChange: (e) => {
                      update(
                        item,
                        updateRuleItem(item, rule._id, {
                          class: e,
                          value: ["all"]
                        })
                      );
                    }
                  }
                ),
                /* @__PURE__ */ import_react.default.createElement(
                  import_antd.Select,
                  {
                    ...config.rule,
                    value: rule.operator,
                    onChange: (e) => {
                      update(
                        item,
                        updateRuleItem(item, rule._id, {
                          operator: e
                        })
                      );
                    }
                  }
                ),
                /* @__PURE__ */ import_react.default.createElement(
                  import_antd.Select,
                  {
                    ...config.options,
                    options: ((_a2 = config.type.options.find(
                      (d) => d.value === rule.class
                    )) == null ? void 0 : _a2.list) || [],
                    value: rule.value,
                    onChange: (e, option) => {
                      console.log("options", option);
                      update(
                        item,
                        updateRuleItem(item, rule._id, {
                          value: filterAllOption(e, rule.value)
                        })
                      );
                    }
                  }
                ),
                item.rule.length > 1 ? /* @__PURE__ */ import_react.default.createElement(
                  import_icons.CloseOutlined,
                  {
                    onClick: () => {
                      update(item, {
                        ...item,
                        rule: item.rule.filter(
                          (d) => d._id !== rule._id
                        )
                      });
                    }
                  }
                ) : null
              );
            })))
          );
        },
        renderAddButton: () => null
      }
    )),
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        className: (0, import_classnames.default)("rule-setting-add-button-wrap", {
          ["rule-setting-add-button-wrap-padding"]: value.rule.length > 1
        })
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          size: "large",
          onClick: () => {
            listRef.current.add();
          }
        },
        import_utils3.locales.getText("pisell2.text.add")
      )
    )
  );
};
var ruleSetting_default = RuleSetting;
