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/comment-editor/index.tsx
var comment_editor_exports = {};
__export(comment_editor_exports, {
  default: () => comment_editor_default
});
module.exports = __toCommonJS(comment_editor_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_hooks = require("../_util/hooks");
var import_editor = __toESM(require("./editor"));
var import_emoji_panel = __toESM(require("./emoji-panel"));
var import_tag = __toESM(require("./tag"));
var import_at = __toESM(require("./at"));
var import_utils = require("./utils");
var import_context = require("../config-provider/context");
var import_utils2 = require("../locale/utils");
var import_index = require("./index.less");
var BuiltInControlsMap = {
  emoji: import_emoji_panel.default,
  tag: import_tag.default,
  at: import_at.default
};
var CommentEditor = (props) => {
  const {
    avatar,
    disable: defaultDisable,
    maxLength = Infinity,
    anonymity: defaultAnonymity = false,
    showAnonymity = true,
    controls = ["emoji", "tag", "at"],
    okBtnText,
    renderAction,
    onInput,
    onSubmit,
    getInsideUtils,
    ...rest
  } = props;
  const { uiLocale } = (0, import_react.useContext)(import_context.ConfigContext);
  const [editorFn, setEditorFn] = (0, import_react.useState)();
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [anonymity, setAnonymity] = (0, import_react.useState)(defaultAnonymity);
  (0, import_react.useEffect)(() => {
    editorFn && (getInsideUtils == null ? void 0 : getInsideUtils(editorFn));
  }, [editorFn]);
  const [value, setValue] = (0, import_react.useState)();
  const evChange = (value2) => {
    setValue(value2);
    onInput && onInput(value2);
  };
  const textLength = (0, import_react.useMemo)(() => {
    let html = (value == null ? void 0 : value.html) || "", text = (value == null ? void 0 : value.text) || "", imgCount = (html.match(/<img/g) || []).length, enterCount = (html.match(/<div><br><\/div>/g) || []).length;
    if (html === "<br>" && text === "\n")
      return 0;
    return text.length + imgCount - enterCount;
  }, [value]);
  const disable = (0, import_react.useMemo)(() => {
    if (textLength > maxLength)
      return true;
    if (defaultDisable !== void 0)
      return defaultDisable;
    if (!textLength)
      return true;
    return false;
  }, [defaultDisable, textLength, maxLength]);
  const evSubmit = async () => {
    var _a;
    setLoading(true);
    try {
      let html = (value == null ? void 0 : value.html) || "";
      const { tagList, atList } = (0, import_utils.getCommentInfo)(html);
      let params = { content: html, tagList, notifiedPersonList: atList, anonymity };
      let res = await (onSubmit == null ? void 0 : onSubmit(params));
      if (res) {
        (_a = res.finally) == null ? void 0 : _a.call(res, () => {
          setLoading(false);
          editorFn == null ? void 0 : editorFn.cleanContent();
        });
      } else {
        setLoading(false);
        editorFn == null ? void 0 : editorFn.cleanContent();
      }
    } catch (error) {
      setLoading(false);
    }
  };
  const renderControls = (0, import_react.useMemo)(() => {
    if (!editorFn)
      return null;
    return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, controls.map((Item) => {
      if (typeof Item === "string") {
        let Comp = BuiltInControlsMap[Item];
        return /* @__PURE__ */ import_react.default.createElement(Comp, { ...editorFn });
      }
      return /* @__PURE__ */ import_react.default.createElement(Item, { ...editorFn });
    }));
  }, [controls, editorFn]);
  const renderActions = () => {
    if (renderAction) {
      return renderAction({ textLength, html: (value == null ? void 0 : value.html) || "" });
    }
    return /* @__PURE__ */ import_react.default.createElement(import_antd.Space, { size: 15 }, showAnonymity && /* @__PURE__ */ import_react.default.createElement(import_antd.Checkbox, { onChange: (e) => setAnonymity(e.target.checked), checked: anonymity }, uiLocale.Comment.anonymity), /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", onClick: evSubmit, disabled: disable, loading }, okBtnText ?? uiLocale.Comment.release));
  };
  return (0, import_hooks.useConfig)(
    /* @__PURE__ */ import_react.default.createElement("div", { className: "u-comment-input" }, avatar && /* @__PURE__ */ import_react.default.createElement("div", { className: "avatar" }, avatar), /* @__PURE__ */ import_react.default.createElement("div", { className: "comment-input-main" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "comment-content-detail" }, /* @__PURE__ */ import_react.default.createElement(import_editor.default, { ...rest, setEditorFn, onInput: evChange }), maxLength !== Infinity && /* @__PURE__ */ import_react.default.createElement("div", { className: "comment-content-detail-count" }, textLength > maxLength ? /* @__PURE__ */ import_react.default.createElement("div", null, (0, import_utils2.replaceMessage)(uiLocale.Comment.exceed, { count: `${textLength - maxLength}` })) : /* @__PURE__ */ import_react.default.createElement("div", null, (0, import_utils2.replaceMessage)(uiLocale.Comment.count, { count: `${maxLength - textLength}` })))), /* @__PURE__ */ import_react.default.createElement("div", { className: "controller-wrapper" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Space, { size: 12 }, renderControls), renderActions())))
  );
};
CommentEditor.parseCommentHtml = import_utils.getCommentInfo;
var comment_editor_default = CommentEditor;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
