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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { VtxUpload } from "@vtx/components";
import htmr from "htmr";
import React, { useState } from "react";
import { useSetState } from 'ahooks';
import "./widgets/style.less";
import classNames from "classnames";
import { DeleteOutlined, EyeOutlined } from '@ant-design/icons';
import { useEffect } from "react";
import Viewer from 'viewerjs';
import { getVtxToken } from '@vtx/utils';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
export default function viewRichText(_ref) {
  var content = _ref.content,
    configJson = _ref.configJson,
    onChange = _ref.onChange,
    values = _ref.values,
    _ref$viewMode = _ref.viewMode,
    viewMode = _ref$viewMode === void 0 ? false : _ref$viewMode,
    _ref$configDisabled = _ref.configDisabled,
    configDisabled = _ref$configDisabled === void 0 ? {} : _ref$configDisabled;
  var _useSetState = useSetState({}),
    _useSetState2 = _slicedToArray(_useSetState, 2),
    state = _useSetState2[0],
    setState = _useSetState2[1];
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    imageList = _useState2[0],
    setImageList = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    imageViewer = _useState4[0],
    setImageViewer = _useState4[1];
  function changeValue(obj) {
    if (onChange instanceof Function) {
      onChange(obj);
    }
  }
  useEffect(function () {
    if (imageList) {
      setImageViewer(new Viewer(imageList, {}));
    }
  }, [imageList]);
  useEffect(function () {
    if (values) {
      var configs = JSON.parse(configJson);
      for (var key in values) {
        var config = configs[key],
          node = null;
        if (config.type == 'sign') {
          setState(_defineProperty({}, key, values[key]));
        } else if (['input', 'date', 'textarea'].includes(config.type)) {
          document.getElementById(key).value = values[key];
        } else if (['editable-div'].includes(config.type)) {
          document.getElementById(key).innerHTML = values[key];
        } else if (['radio'].includes(config.type)) {
          node = document.getElementsByName(config.name);
          if (node) for (var i = 0; i < node.length; i++) {
            if (node[i].value == values[key]) {
              node[i].checked = true;
            }
          }
        } else if (['checkbox'].includes(config.type)) {
          node = document.getElementsByName(config.name);
          if (node) for (var _i = 0; _i < node.length; _i++) {
            if (values[key].split("；").includes(node[_i].value)) {
              node[_i].checked = true;
            }
          }
        }
      }
    }
  }, [values]);
  if (!content) {
    return null;
  }
  return htmr(content.replaceAll(' vtx_ck_widgets', ''), {
    transform: {
      _: function _(node, props, children) {
        var obj = {},
          style = {},
          isDisabled = false;
        if (props !== null && props !== void 0 && props.configid) {
          props.id = props.configid;
          var components = JSON.parse(configJson);
          var config = components[props === null || props === void 0 ? void 0 : props.configid] || {};
          for (var key in config) {
            if (key == 'width') {
              style[key] = config[key];
            } else {
              if (key == 'label') {
                obj.placeholder = "\u8BF7\u8F93\u5165".concat(config[key] || '');
              }
              obj[key] = config[key];
            }
          }
          if (obj.type == 'editable-div') {
            obj.contentEditable = true;
            delete props.contentEditable;
          } else if (obj.type == 'date') {
            style.width = style.width || '100px';
          }
          obj.style = style;
          obj.id = props === null || props === void 0 ? void 0 : props.configid;
          var temp = configDisabled[props.configid];
          if (temp !== null && temp !== void 0 && temp.useOtherField && !(temp !== null && temp !== void 0 && temp.editAble)) {
            isDisabled = true;
          }
          if (viewMode || isDisabled) {
            obj.disabled = true;
            delete obj.contentEditable;
          }
        }
        if (node == 'label' && ["radio", 'checkbox'].includes(props.type)) {
          var data = [];
          if (obj.url) $.ajax({
            url: "".concat(obj.url).concat(obj.url.indexOf("?") > -1 ? '&' : '?', "token=").concat(getVtxToken("token")),
            async: false,
            success: function success(res) {
              if (res.result == 0) {
                data = res.data || [];
              }
            }
          });
          return /*#__PURE__*/_jsx(_Fragment, {
            children: data.map(function (item) {
              return /*#__PURE__*/_jsxs("label", {
                children: [/*#__PURE__*/_jsx("input", {
                  type: props.type,
                  name: obj.name,
                  value: item.parmName,
                  disabled: viewMode || isDisabled
                }), item.parmName]
              }, item.id);
            })
          });
        }
        if (node == 'span' && ['sign'].includes(obj.type)) {
          var arr = JSON.parse(state[props.configid] || '[]');
          if (arr.length > 0) {
            return /*#__PURE__*/_jsxs("span", {
              className: classNames('sign_pic_box'),
              ref: function ref(ins) {
                if (ins) setImageList(ins);
              },
              children: [/*#__PURE__*/_jsx("img", {
                className: classNames('sign_pic'),
                src: "/cloudFile/common/downloadFile?id=".concat(arr[0].id)
              }), /*#__PURE__*/_jsxs("span", {
                className: classNames('sign_opera'),
                children: [/*#__PURE__*/_jsx(EyeOutlined, {
                  className: classNames('sign_del'),
                  onClick: function onClick() {
                    if (imageViewer) {
                      imageViewer.view(0);
                    }
                  }
                }), !viewMode && !isDisabled && /*#__PURE__*/_jsx(DeleteOutlined, {
                  className: classNames('sign_del'),
                  onClick: function onClick() {
                    setState(_defineProperty({}, props.configid, ''));
                    changeValue(_defineProperty({}, props.configid, ''));
                  }
                })]
              })]
            });
          }
          if (viewMode || isDisabled) {
            return /*#__PURE__*/React.createElement(node, _objectSpread(_objectSpread({}, obj), props), children);
          }
          return /*#__PURE__*/_jsx(VtxUpload, {
            accept: ".jpg,.png,.jepg",
            customizedButton: /*#__PURE__*/React.createElement(node, _objectSpread(_objectSpread({}, obj), props), children),
            maxNum: 1,
            onSuccess: function onSuccess(file) {
              var obj = _defineProperty({}, props.configid, JSON.stringify([{
                id: file.id,
                name: file.name,
                type: file.type
              }]));
              setState(obj);
              changeValue(obj);
            }
          });
        }
        if (!props && !children) {
          return node;
        }
        if (node == 'table') {
          return /*#__PURE__*/React.createElement(node, _objectSpread(_objectSpread(_objectSpread({}, obj), props), {}, {
            border: 'true'
          }), children);
        }
        if (node === 'input' && obj.type === 'date') {
          if (viewMode && !(values !== null && values !== void 0 && values[obj.id])) {
            return /*#__PURE__*/_jsx("span", {
              id: obj.id
            });
          }
        }
        return /*#__PURE__*/React.createElement(node, _objectSpread(_objectSpread({}, obj), props), children);
      }
    }
  });
}