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/booking/info2/service/editService/ResourcesV2/index.tsx
var ResourcesV2_exports = {};
__export(ResourcesV2_exports, {
  default: () => ResourcesV2_default
});
module.exports = __toCommonJS(ResourcesV2_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_antd = require("antd");
var import_materials = require("@pisell/materials");
var import_MaxLengthSelect = __toESM(require("./MaxLengthSelect"));
var import_like = __toESM(require("../../../../info/service/like"));
var import_index = require("./index.less");
var import_utils = require("@pisell/utils");
var import_utilsByBooking = require("../../../utilsByBooking");
var import_lodash = require("lodash");
var import_utils2 = require("../utils");
var import_ahooks = require("ahooks");
var import_icons = require("@ant-design/icons");
var Resources = ({
  state,
  cacheItem,
  originCacheItem,
  getCurrentCacheItem,
  onChange,
  value,
  originValue,
  config = {
    // 是否显示自动分配
    autoAllocation: true,
    // 是否显示资源错误图标
    resourceErrorIcon: true,
    disabled: false,
    size: "large"
  }
}, ref) => {
  var _a, _b, _c;
  const [showError, setShowError] = (0, import_react.useState)(false);
  const update = (0, import_ahooks.useUpdate)();
  const data = (0, import_react.useMemo)(() => {
    var _a2;
    return ((_a2 = cacheItem == null ? void 0 : cacheItem._data) == null ? void 0 : _a2.resources) || [];
  }, [(_a = cacheItem == null ? void 0 : cacheItem._data) == null ? void 0 : _a.resources]);
  const originData = (0, import_react.useMemo)(() => {
    var _a2;
    return ((_a2 = cacheItem == null ? void 0 : cacheItem._data) == null ? void 0 : _a2.resources) || [];
  }, [(_b = originCacheItem == null ? void 0 : originCacheItem._data) == null ? void 0 : _b.resources]);
  const resourceMaps = (0, import_react.useMemo)(() => {
    var _a2;
    return ((_a2 = cacheItem == null ? void 0 : cacheItem._data) == null ? void 0 : _a2.resourceMaps) || [];
  }, [(_c = cacheItem == null ? void 0 : cacheItem._data) == null ? void 0 : _c.resourceMaps]);
  const defaultValue = (0, import_react.useMemo)(() => {
    return data.reduce((pre, cur2) => {
      pre[cur2.form_id] = [];
      return pre;
    }, {});
  }, [data]);
  const _value = (0, import_react.useMemo)(() => {
    return value || defaultValue;
  }, [value, defaultValue]);
  const handleCheckAutoAllocation = (id, checked) => {
    (0, import_utils2.setAutoAllocationStorage)({ [id]: checked });
    update();
  };
  const _onChange = (val, form_id, key) => {
    let _val = { ..._value };
    let maps = _val[form_id] ? _val[form_id].reduce((pre, cur2) => {
      pre[cur2.relation_id] = cur2;
      return pre;
    }, {}) : {};
    _val[form_id] = val.map((d) => {
      var _a2, _b2, _c2, _d2, _e, _f, _g, _h;
      let _d = maps[d];
      if (_d) {
        if (key === "like_status") {
          d[key].like_status = val;
        }
      }
      const currentForm = (resourceMaps == null ? void 0 : resourceMaps[form_id]) || {};
      return (
        // maps[d] 为真时, 说明是之前已选择, 否则为新增的值,补充数据
        maps[d] || {
          relation_type: ((_b2 = (_a2 = currentForm.renderListMaps[d]) == null ? void 0 : _a2.usable) == null ? void 0 : _b2.relation_type) || "form",
          form_id,
          relation_id: d,
          like_status: "common",
          children: (_d2 = (_c2 = currentForm.renderListMaps) == null ? void 0 : _c2[d]) == null ? void 0 : _d2.children,
          // 新添加的数据补充错误信息
          errors: (0, import_utilsByBooking.getErrorList)(
            (_e = currentForm.renderListMaps) == null ? void 0 : _e[d],
            getCurrentCacheItem()
          ),
          metadata: {
            ...(_g = (_f = currentForm.renderListMaps) == null ? void 0 : _f[d]) == null ? void 0 : _g.metadata,
            form_name: currentForm.title,
            resource_name: (_h = currentForm.renderListMaps[d]) == null ? void 0 : _h.main_field
          }
        }
      );
    });
    setShowError(false);
    onChange == null ? void 0 : onChange(_val);
  };
  const setLikeStatus = (val, form_id, key) => {
    var _a2;
    let _val = { ..._value };
    if ((_a2 = _val == null ? void 0 : _val[form_id]) == null ? void 0 : _a2[0]) {
      _val[form_id][0][key] = val;
      onChange == null ? void 0 : onChange(_val);
    }
  };
  const renderOptions = (0, import_react.useCallback)(
    (array, value2 = "value", label = "label", type) => {
      return array.map((d) => {
        let props = {};
        if (type === "resource") {
          props.labelText = d.labelText;
        }
        return /* @__PURE__ */ import_react.default.createElement(
          import_antd.Select.Option,
          {
            value: d[value2],
            key: d[value2],
            ...props,
            relation_type: d.relation_type
          },
          d[label]
        );
      });
    },
    [config == null ? void 0 : config.resourceErrorIcon]
  );
  const check = () => {
    var _a2;
    for (let item of data) {
      let _length = (_a2 = _value[item.form_id]) == null ? void 0 : _a2.length;
      if (item.select_type.min > _length) {
        setShowError(true);
        return false;
      }
    }
    return true;
  };
  (0, import_react.useImperativeHandle)(ref, () => {
    return {
      check
    };
  });
  const getIsAutoAllocation = (id) => {
    return (0, import_utils2.getAutoAllocationStorage)(id);
  };
  const renderItem = (item, isOrigin) => {
    var _a2;
    if (!item) return null;
    const disabled = isOrigin || (config == null ? void 0 : config.disabled);
    const cur2 = isOrigin ? originValue[item.form_id] : _value[item.form_id];
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Col, { flex: "1 0" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "resource-v2 resource-col" }, item.select_type.type === "multiple" ? null : /* @__PURE__ */ import_react.default.createElement(
      import_antd.Button,
      {
        className: (0, import_classnames.default)(
          "resource-col-like-wrap",
          {
            "resource-col-like-wrap-disabled": disabled
          },
          (config == null ? void 0 : config.size) || "large"
        ),
        size: config == null ? void 0 : config.size,
        onClick: () => {
          var _a3;
          if (disabled) {
            return;
          }
          setLikeStatus(
            ((_a3 = cur2 == null ? void 0 : cur2[0]) == null ? void 0 : _a3.like_status) === "like" ? "common" : "like",
            item.form_id,
            "like_status"
          );
        }
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_like.default,
        {
          status: (_a2 = cur2 == null ? void 0 : cur2[0]) == null ? void 0 : _a2.like_status,
          className: `${disabled ? "like-icon-disabled" : ""}`
        }
      )
    ), /* @__PURE__ */ import_react.default.createElement(
      import_MaxLengthSelect.default,
      {
        className: "resource-col-select",
        size: (config == null ? void 0 : config.size) || "large",
        maxLength: item.select_type.max,
        mode: item.select_type.type === "multiple" ? "multiple" : void 0,
        onChange: (e) => {
          let val = e;
          if (!(0, import_lodash.isArray)(e)) {
            if (e || e === 0) {
              val = [e];
            }
          }
          _onChange(val, item.form_id, "value");
        },
        value: cur2 == null ? void 0 : cur2.map((d) => d.relation_id),
        showSearch: true,
        filterOption: (str, option) => {
          return ((option == null ? void 0 : option.labelText) || "").toLowerCase().includes((str || "").toLowerCase());
        },
        disabled
      },
      renderOptions(
        item.renderList,
        "id",
        (config == null ? void 0 : config.resourceErrorIcon) ? "label" : "labelText",
        "resource"
      )
    ), (config == null ? void 0 : config.autoAllocation) ? /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Checkbox,
      {
        checked: getIsAutoAllocation(item.form_id),
        onChange: (e) => {
          handleCheckAutoAllocation(item.form_id, e.target.checked);
        }
      }
    ), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Tooltip,
      {
        title: import_utils.locales.getText(
          "page.booking.text.resource.check-auto-allocation-tip"
        )
      },
      /* @__PURE__ */ import_react.default.createElement(import_icons.QuestionCircleOutlined, null)
    )) : null)));
  };
  const renderErrors = (item) => {
    const cur2 = _value[item.form_id];
    const errors = (cur2 || []).reduce((pre, d) => {
      return pre.concat((d == null ? void 0 : d.errors) || []);
    }, []);
    return state.renderType !== "eventBooking" && (errors || []).map((d) => {
      return /* @__PURE__ */ import_react.default.createElement(
        import_antd.Alert,
        {
          key: d.type,
          message: d.label,
          type: "warning",
          showIcon: true,
          style: { marginBottom: 12 }
        }
      );
    });
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, data.map((item) => {
    const originItem = originData == null ? void 0 : originData.find(
      (originDataItem) => originDataItem.form_id === item.form_id
    );
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key: item.form_id }, /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        label: item.title,
        required: true,
        labelCol: config == null ? void 0 : config.labelCol,
        extra: showError && item.select_type.min > (cur == null ? void 0 : cur.length) ? /* @__PURE__ */ import_react.default.createElement("span", { style: { color: "#ff4d4f" } }, import_utils.locales.getText(
          "page.booking.text.resource.min-length-error"
        )(item.select_type.min, item.title)) : void 0
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Row, { gutter: [16, 16], wrap: false }, renderItem(originItem, true), !!originItem ? /* @__PURE__ */ import_react.default.createElement(
        import_antd.Col,
        {
          flex: "none",
          style: { display: "flex", alignItems: "center" }
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_materials.Iconfont,
          {
            type: "pisell2-arrow-block-right",
            style: { fontSize: 24, color: "#1b1b1b" }
          }
        )
      ) : null, renderItem(item, false))
    ), renderErrors(item));
  }));
};
var ResourcesV2_default = (0, import_react.forwardRef)(Resources);
