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/feedback/AddFeedbackModal.tsx
var AddFeedbackModal_exports = {};
__export(AddFeedbackModal_exports, {
  default: () => AddFeedbackModal
});
module.exports = __toCommonJS(AddFeedbackModal_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_zh_CN = __toESM(require("antd/es/locale/zh_CN"));
var import_en_US = __toESM(require("antd/es/locale/en_US"));
var import_icons = require("@ant-design/icons");
var import_interface = require("./interface");
var import_context = require("../config-provider/context");
var import_utils = require("../locale/utils");
var import_index = require("./index.less");
var { TextArea } = import_antd.Input;
var { Dragger } = import_antd.Upload;
var LAYOUT = {
  labelCol: { span: 4 },
  wrapperCol: { span: 18 }
};
var BASE_URL = "https://overmind-project.netease.com";
var AddFeedbackModal = class extends import_react.Component {
  constructor(props) {
    super(props);
    this.hideModal = () => {
      var _a, _b;
      this.setState({ visible: false });
      (_b = (_a = this.props).onCancel) == null ? void 0 : _b.call(_a);
    };
    this.onChangeBug = () => {
      this.setState({ type: import_interface.FeedbackType.Bug }, () => {
        this.formRef.current.resetFields();
      });
    };
    this.onChangeAdvice = () => {
      this.setState({ type: import_interface.FeedbackType.Advice }, () => {
        this.formRef.current.resetFields();
      });
    };
    this.onOk = async (uiLocale) => {
      try {
        const {
          config: { appkey, email, level = 2 },
          config,
          onOk
        } = this.props;
        const { type } = this.state;
        const values = await this.formRef.current.validateFields();
        const { attachments } = values;
        if (attachments) {
          const jsonArr = attachments == null ? void 0 : attachments.filter((file) => file.percent === 100).map((file) => file.response.result);
          Object.assign(values, { attachments: JSON.stringify(jsonArr) });
        }
        this.setState({
          btnLoading: true
        });
        const url = type === import_interface.FeedbackType.Advice ? "/api/advise/add4Everyone" : "/api/ticket/add4Everyone";
        const res = await fetch(`${BASE_URL}${url}`, {
          method: "post",
          headers: {
            "Content-Type": "application/json;charset=utf-8",
            appkey,
            email
          },
          body: JSON.stringify({
            ...config,
            level,
            env: "online-project",
            submitEmail: email,
            ...values
          })
        });
        const { code, msg } = await res.json();
        if (code === 200) {
          this.hideModal();
          this.formRef.current.resetFields();
          import_antd.message.success(uiLocale.Feedback.submitSuccess);
          onOk && onOk(type);
        } else {
          import_antd.message.error(msg);
        }
      } catch (e) {
        console.log(e);
      } finally {
        this.setState({
          btnLoading: false
        });
      }
    };
    this.beforeUpload = (file, fileList, uiLocale) => {
      for (const item of fileList) {
        const { size, name } = item;
        if (size >= 40 * 1024 * 1024) {
          import_antd.message.error((0, import_utils.replaceMessage)(uiLocale.Feedback.uploadError, { name }));
          return import_antd.Upload.LIST_IGNORE;
        }
      }
      return true;
    };
    this.onUpload = (info) => {
      try {
        const { response, status } = info.file;
        if (status === "error") {
          throw response.msg;
        }
        if (status === "done") {
          const { code } = response;
          if (code !== 200 && code !== "200") {
            throw response.msg;
          }
        }
      } catch (error) {
        console.log(error);
      }
    };
    this.normFile = (e) => {
      if (Array.isArray(e)) {
        return e;
      }
      return e && e.fileList;
    };
    this.getInitialValues = (type) => {
      const { defaultValues } = this.props;
      const defaultKeyValueMap = {
        [import_interface.FeedbackType.Bug]: {
          name: defaultValues == null ? void 0 : defaultValues.bugName,
          description: defaultValues == null ? void 0 : defaultValues.bugDescription
        },
        [import_interface.FeedbackType.Advice]: {
          name: defaultValues == null ? void 0 : defaultValues.adviceName,
          description: defaultValues == null ? void 0 : defaultValues.adviceDescription
        }
      };
      return defaultKeyValueMap[type];
    };
    this.state = {
      type: props.type || import_interface.FeedbackType.Advice,
      visible: !!props.visible,
      btnLoading: false,
      props
    };
    this.formRef = import_react.default.createRef();
  }
  static getDerivedStateFromProps(nextProps, prevState) {
    const { visible, type } = nextProps;
    if (visible !== prevState.visible && visible !== prevState.props.visible) {
      return {
        visible,
        props: nextProps
      };
    }
    if (type !== prevState.type && type !== prevState.props.type) {
      return {
        type,
        props: nextProps
      };
    }
    return {
      props: nextProps
    };
  }
  render() {
    const { visible, btnLoading, type } = this.state;
    const { name, description } = this.getInitialValues(type);
    return /* @__PURE__ */ import_react.default.createElement(import_context.ConfigContext.Consumer, null, (value) => {
      var _a;
      const { uiLocale, theme = { token: { colorPrimary: "#1677ff" } } } = value;
      const TYPE_INFO = {
        [import_interface.FeedbackType.Advice]: uiLocale.Feedback.Advice,
        [import_interface.FeedbackType.Bug]: uiLocale.Feedback.Bug
      };
      const defaultLayout = uiLocale.locale === "zh" ? LAYOUT : {};
      const {
        prefixCls,
        typeInfo = TYPE_INFO,
        btnText,
        config: { appkey, email },
        bugFirst,
        layout = defaultLayout
      } = this.props;
      const {
        Tips,
        Title: { Label: TitleLabel, Placeholder: TitlePlaceholder },
        Description: { Label: DescLabel, Placeholder: DescPlaceholder },
        Attachments: { Label: AttachLabel },
        TypeLabel: { FormLabel, AdviceLabel, AdviceDesc, TicketLabel, TicketDesc }
      } = (typeInfo == null ? void 0 : typeInfo[type]) ?? TYPE_INFO[type];
      return /* @__PURE__ */ import_react.default.createElement(import_antd.ConfigProvider, { ...value, prefixCls: prefixCls ? prefixCls : value.prefixCls, locale: uiLocale.locale === "zh" ? import_zh_CN.default : import_en_US.default }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Modal,
        {
          className: "ehr-addFeedback-modal",
          title: btnText || uiLocale.Feedback.btnText,
          open: visible,
          destroyOnClose: true,
          width: 800,
          maskClosable: false,
          onCancel: this.hideModal,
          onOk: () => this.onOk(uiLocale),
          okButtonProps: {
            loading: btnLoading
          },
          style: { "--primary-color": (_a = theme == null ? void 0 : theme.token) == null ? void 0 : _a.colorPrimary }
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { ref: this.formRef, layout: uiLocale.locale === "zh" ? "horizontal" : "vertical", ...layout }, /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: FormLabel, required: true }, /* @__PURE__ */ import_react.default.createElement("div", { className: `ehr-feedback-type-container ${bugFirst ? "ehr-feedback-type-container-reverse" : ""}` }, /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: `ehr-feedback-type ${type === import_interface.FeedbackType.Advice ? "ehr-feedback-type-selected" : ""}`,
            onClick: this.onChangeAdvice
          },
          /* @__PURE__ */ import_react.default.createElement(import_icons.BulbFilled, { className: "ehr-type-icon" }),
          /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "feedback-type-title" }, AdviceLabel), /* @__PURE__ */ import_react.default.createElement("div", { className: "feedback-type-subTitle" }, AdviceDesc))
        ), /* @__PURE__ */ import_react.default.createElement(
          "div",
          {
            className: `ehr-feedback-type ${type === import_interface.FeedbackType.Bug ? "ehr-feedback-type-selected" : ""}`,
            onClick: this.onChangeBug
          },
          /* @__PURE__ */ import_react.default.createElement(import_icons.BugFilled, { className: "ehr-type-icon" }),
          /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement("div", { className: "feedback-type-title" }, TicketLabel), /* @__PURE__ */ import_react.default.createElement("div", { className: "feedback-type-subTitle" }, TicketDesc))
        ))), /* @__PURE__ */ import_react.default.createElement("div", { className: "ehr-feedback-tips" }, /* @__PURE__ */ import_react.default.createElement(import_icons.ExclamationCircleOutlined, { className: "tip-icon" }), /* @__PURE__ */ import_react.default.createElement("div", { className: "ehr-feedback-tips-content", dangerouslySetInnerHTML: { __html: Tips } })), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: TitleLabel, name: "name", initialValue: name, required: true, rules: [{ required: true }] }, /* @__PURE__ */ import_react.default.createElement(import_antd.Input, { placeholder: TitlePlaceholder, maxLength: 100 })), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: DescLabel, name: "description", initialValue: description }, /* @__PURE__ */ import_react.default.createElement(TextArea, { rows: 4, placeholder: DescPlaceholder, maxLength: 500 })), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, { label: AttachLabel, name: "attachments", valuePropName: "fileList", getValueFromEvent: this.normFile }, /* @__PURE__ */ import_react.default.createElement(
          Dragger,
          {
            multiple: true,
            beforeUpload: (file, fileList) => this.beforeUpload(file, fileList, uiLocale),
            onChange: this.onUpload,
            action: `${BASE_URL}/api/upload`,
            accept: "image/jpeg,image/png",
            headers: {
              appkey,
              email
            }
          },
          /* @__PURE__ */ import_react.default.createElement("p", null, /* @__PURE__ */ import_react.default.createElement(import_icons.InboxOutlined, { className: "ehr-feedback-upload" })),
          /* @__PURE__ */ import_react.default.createElement("p", null, uiLocale.Feedback.uploadHint)
        )))
      ));
    });
  }
};
AddFeedbackModal.defaultProps = {
  defaultValues: {}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
