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 React, { useEffect, useMemo, useState } from "react";
import moment from 'moment';
import { Image } from 'antd';
import { getBase64 } from "../Uploader/util";
import useUploaderStyle from "./style/UploaderStyle";
import classNames from 'classnames';
// 设置纯展示模式下的级联选择显示值

var setCascaderValue = function setCascaderValue(value, props) {
  var selectOptions = props.selectOptions,
    defaultProps = props.defaultProps;
  if (!value || value.length === 0 || !selectOptions) return '-'; // 无值情况 下
  var matchNode = undefined;
  var matchValueItems = [];
  value.forEach(function (valKey) {
    var matchValue;
    // 第一个值使用  ‘selectOptions’查找，后面使用上一次匹配到的父节点查找 matchNode
    if (matchNode && matchNode.children) {
      matchValue = matchNode.children.filter(function (seItem) {
        return seItem.value == valKey;
      });
    } else {
      matchValue = selectOptions.filter(function (seItem) {
        return seItem.value == valKey;
      });
    }
    if (matchValue.length > 0) {
      matchNode = matchValue[0];
      matchValueItems.push(matchValue[0]);
    }
  });
  // 如果自定义"选择后展示的渲染函数"则直接返回此选择方法
  if (defaultProps.displayRender) return defaultProps.displayRender(matchValueItems.map(function (item) {
    return item.label;
  }), matchValueItems);
  return matchValueItems.map(function (item) {
    return item.label;
  }).join('/');
};

// 设置纯展示模式下的下拉、单选、多选表单值
var setSelectValue = function setSelectValue(value, props) {
  var selectOptions = props.selectOptions,
    selectOptionRender = props.selectOptionRender,
    defaultProps = props.defaultProps,
    type = props.type,
    separator = props.separator;
  var labelInValue = defaultProps.labelInValue;
  if (!value && value !== 0 || !selectOptions) return '-'; // 无值情况 下
  // 数组情况
  if (Array.isArray(value)) {
    if (value.length == 0) {
      return '-'; // 无值情况 下
    }
    // 返回是数组
    var matchValues = [];
    value.forEach(function (valKey, index) {
      var matchValue = selectOptions.filter(function (seItem) {
        return seItem.value == (labelInValue ? valKey.value : valKey);
      });
      if (matchValue.length > 0) {
        // 如果 select 配置选项展示内容，则直接使用其配置值展示当前只读信息
        if (selectOptionRender) {
          matchValues.push( /*#__PURE__*/React.createElement("div", {
            key: index,
            className: "checkbox-viewonly-item"
          }, selectOptionRender(matchValue[0]), value.length - 1 > index ? separator ? separator : separator == null ? '' : ',' : ''));
        } else {
          matchValues.push(matchValue[0].label);
        }
      }
    });
    // 如果 select 配置选项展示内容，则直接使用其配置值展示当前只读信息
    if (selectOptionRender) {
      return /*#__PURE__*/React.createElement("div", {
        className: "checkbox-viewonly-list"
      }, matchValues);
    }
    return matchValues.join(separator ? separator : ',');
  } else {
    // 如select配置labelInValue则value返回数据是对象{key: 'xxx'}
    var matchValue = selectOptions.filter(function (seItem) {
      return seItem.value == (labelInValue ? value.value : value);
    });
    // 如果 select 配置选项展示内容，则直接使用其配置值展示当前只读信息
    if (selectOptionRender) {
      return matchValue.length > 0 ? selectOptionRender(matchValue[0]) : '-';
    }
    return matchValue.length > 0 ? matchValue[0].label : '-';
  }
};
// 设置纯展示模式下的日期控件值
var setDatePickerValue = function setDatePickerValue(value, props) {
  var type = props.type,
    protoConfig = props.protoConfig;
  if (!value && value !== 0) return '-'; // 无值情况 下
  // 数组情况 'YYYY-MM-DD HH:mm:ss' : 'YYYY-MM-DD'
  switch (type) {
    case 'datePicker':
      return moment(value).format(protoConfig && protoConfig.format || 'YYYY-MM-DD');
      break;
    case 'dateTimePicker':
      return moment(value).format(protoConfig && protoConfig.format || 'YYYY-MM-DD HH:mm:ss');
      break;
    case 'rangePicker':
      return "".concat(moment(value[0]).format(protoConfig && protoConfig.format || 'YYYY-MM-DD'), " - ").concat(moment(value[1]).format(protoConfig && protoConfig.format || 'YYYY-MM-DD'));
      break;
    case 'rangeTimePicker':
      return "".concat(moment(value[0]).format(protoConfig && protoConfig.format || 'YYYY-MM-DD  HH:mm:ss'), " - ").concat(moment(value[1]).format(protoConfig && protoConfig.format || 'YYYY-MM-DD  HH:mm:ss'));
      break;
    case 'monthPicker':
      return moment(value).format(protoConfig && protoConfig.format || 'MM月');
      break;
    default:
      return '-';
      break;
  }
};

// 设置纯展示模式下的时间控件值
var setTimePickerValue = function setTimePickerValue(value, props) {
  var type = props.type,
    protoConfig = props.protoConfig;
  if (!value && value !== 0) return '-'; // 无值情况 下
  switch (type) {
    case 'timePicker':
      return moment(value).format(protoConfig && protoConfig.format || 'HH:mm:ss');
      break;
    case 'timePickerRange':
      return "".concat(moment(value[0]).format(protoConfig && protoConfig.format || 'HH:mm:ss'), " - ").concat(moment(value[1]).format(protoConfig && protoConfig.format || 'HH:mm:ss'));
      break;
    default:
      return '-';
      break;
  }
};
// 设置纯展示模式下上传控件显示值
var setUploaderValue = function setUploaderValue(value, props) {
  var componentName = 'spec-uploader-viewonly';
  var _useUploaderStyle = useUploaderStyle(componentName),
    _useUploaderStyle2 = _slicedToArray(_useUploaderStyle, 2),
    wrapSSR = _useUploaderStyle2[0],
    hashId = _useUploaderStyle2[1];
  if (!value && value !== 0) return '-'; // 无值情况 下
  if (Array.isArray(value)) {
    var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      previews = _useState2[0],
      setPreviews = _useState2[1];
    useEffect(function () {
      var previewsUlr = [];
      value.forEach(function (vItem) {
        if (vItem !== null && vItem !== void 0 && vItem.downloadUrl) {
          previewsUlr.push({
            url: vItem.downloadUrl
          });
          setPreviews(previewsUlr.filter(function (pItem) {
            return pItem;
          }));
        } else {
          if (vItem !== null && vItem !== void 0 && vItem.originFileObj) getBase64(vItem.originFileObj).then(function (result) {
            previewsUlr.push(result);
            setPreviews(previewsUlr.filter(function (pItem) {
              return pItem;
            }));
            return result;
          });
        }
      });
    }, [value]);
    return wrapSSR( /*#__PURE__*/React.createElement("div", {
      className: classNames('spec-uploader-viewonly', hashId)
    }, /*#__PURE__*/React.createElement(Image.PreviewGroup, null, previews && (previews === null || previews === void 0 ? void 0 : previews.map(function (item, index) {
      return item !== null && item !== void 0 && item.url ? /*#__PURE__*/React.createElement(Image, {
        key: index,
        width: 100,
        height: 100,
        style: {
          objectFit: 'contain'
        },
        src: item.url
      }) : /*#__PURE__*/React.createElement("div", {
        key: index,
        className: "spec-uploader-viewonly-item"
      }, "\u6682\u65E0\u56FE\u7247");
    })))));
  } else {
    var _useState3 = useState(),
      _useState4 = _slicedToArray(_useState3, 2),
      preview = _useState4[0],
      setPreview = _useState4[1];
    useEffect(function () {
      getBase64(value).then(function (result) {
        setPreview(result);
      });
    }, [value]);
    return wrapSSR( /*#__PURE__*/React.createElement("div", {
      className: classNames('spec-uploader-viewonly', hashId)
    }, preview ? /*#__PURE__*/React.createElement(Image, {
      style: {
        objectFit: 'contain'
      },
      width: 100,
      src: preview === null || preview === void 0 ? void 0 : preview.url
    }) : '暂无图片'));
  }
};

// 设置输入框类纯展示显示值
var setInputValue = function setInputValue(value, props) {
  var type = props.type,
    _props$viewOnlyHides = props.viewOnlyHides,
    viewOnlyHides = _props$viewOnlyHides === void 0 ? [] : _props$viewOnlyHides;
  var defaultText = '';
  if (type === 'input' || type === 'search' || type === 'mentions') {
    defaultText = value || '-';
  }
  if (type === 'inputNumber') {
    defaultText = value || value === 0 ? value : '-';
  }
  // 只读下隐藏扩展的结构 viewOnlyHides: ['addonBefore', 'prefix', 'suffix', 'addonAfter']
  return /*#__PURE__*/React.createElement(React.Fragment, null, !(viewOnlyHides !== null && viewOnlyHides !== void 0 && viewOnlyHides.includes('addonBefore')) ? props === null || props === void 0 ? void 0 : props.addonBefore : '', " ", !(viewOnlyHides !== null && viewOnlyHides !== void 0 && viewOnlyHides.includes('prefix')) ? props === null || props === void 0 ? void 0 : props.prefix : '', " ", defaultText, " ", !(viewOnlyHides !== null && viewOnlyHides !== void 0 && viewOnlyHides.includes('suffix')) ? props === null || props === void 0 ? void 0 : props.suffix : '', " ", !(viewOnlyHides !== null && viewOnlyHides !== void 0 && viewOnlyHides.includes('addonAfter')) ? props === null || props === void 0 ? void 0 : props.addonAfter : '');
};
export { setCascaderValue, setSelectValue, setDatePickerValue, setTimePickerValue, setUploaderValue, setInputValue };