"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getItemClass = getItemClass;
exports.getItemContentClass = getItemContentClass;
exports.renderItemContent = renderItemContent;
exports.renderItemErrorIcon = renderItemErrorIcon;
exports.renderTitle = renderTitle;
var _ui = require("../../ui");
var _utils = require("../../ui/src/utils");
var _dom = require("../../ui/src/dom");
var _vn = require("../../ui/src/vn");
var _util = require("./util");
var _tooltip = _interopRequireDefault(require("../../tooltip"));
var _icon = _interopRequireDefault(require("../../icon"));
var _xeUtils = _interopRequireDefault(require("xe-utils"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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 renderPrefixIcon(h, titlePrefix) {
  return h('span', {
    class: 'vxe-form--item-title-tip-prefix'
  }, [h(_icon.default, {
    class: titlePrefix.icon || (0, _ui.getIcon)().FORM_PREFIX,
    props: {
      status: titlePrefix.iconStatus
    }
  })]);
}
function renderSuffixIcon(h, titleSuffix) {
  return h('span', {
    class: 'vxe-form--item-title-tip-suffix'
  }, [h(_icon.default, {
    class: titleSuffix.icon || (0, _ui.getIcon)().FORM_SUFFIX,
    props: {
      status: titleSuffix.iconStatus
    }
  })]);
}
function getItemClass($xeForm, item, isGroup) {
  var _ref;
  var formProps = $xeForm;
  var formReactData = $xeForm.reactData;
  var $xeGrid = $xeForm.$xeGrid;
  var data = formProps.data,
    rules = formProps.rules,
    readonly = formProps.readonly,
    disabled = formProps.disabled,
    allSpan = formProps.span,
    allTitleBackground = formProps.titleBackground,
    allTitleBold = formProps.titleBold,
    allTitleColon = formProps.titleColon,
    allTitleAsterisk = formProps.titleAsterisk,
    allVertical = formProps.vertical,
    allPadding = formProps.padding;
  var collapseAll = formReactData.collapseAll;
  var folding = item.folding,
    field = item.field,
    itemRender = item.itemRender,
    showError = item.showError,
    className = item.className,
    vertical = item.vertical,
    padding = item.padding,
    children = item.children,
    showContent = item.showContent;
  var vSize = $xeForm.computeSize;
  var validOpts = $xeForm.computeValidOpts;
  var showErrorMessage = validOpts.showErrorMessage,
    showMessage = validOpts.showMessage,
    showErrorBackground = validOpts.showErrorBackground,
    showErrorIcon = validOpts.showErrorIcon;
  var compConf = (0, _utils.isEnableConf)(itemRender) ? _ui.renderer.get(itemRender.name) : null;
  var itemClassName = compConf ? compConf.formItemClassName || compConf.itemClassName : '';
  var span = item.span || allSpan;
  var itemPadding = _xeUtils.default.eqNull(padding) ? allPadding : padding;
  var itemVertical = _xeUtils.default.eqNull(vertical) ? allVertical : vertical;
  var titleBackground = _xeUtils.default.eqNull(item.titleBackground) ? allTitleBackground : item.titleBackground;
  var titleBold = _xeUtils.default.eqNull(item.titleBold) ? allTitleBold : item.titleBold;
  var titleColon = _xeUtils.default.eqNull(item.titleColon) ? allTitleColon : item.titleColon;
  var titleAsterisk = _xeUtils.default.eqNull(item.titleAsterisk) ? allTitleAsterisk : item.titleAsterisk;
  var params = {
    data: data,
    disabled: disabled,
    readonly: readonly,
    field: field,
    property: field,
    item: item,
    $form: $xeForm,
    $grid: $xeGrid
  };
  var hasGroup = children && children.length > 0;
  var isRequired = false;
  var isValid = false;
  if (!readonly && rules) {
    var itemRules = item.rules ? item.rules : rules[field];
    if (itemRules && itemRules.length) {
      isValid = true;
      isRequired = itemRules.some(function (rule) {
        return rule.required;
      });
    }
  }
  return [isGroup || hasGroup ? 'vxe-form--group' : '', 'vxe-form--item', item.id, span ? "vxe-form--item-col_".concat(span, " is--span") : '', "".concat((_xeUtils.default.isBoolean(showErrorMessage) ? showErrorMessage : showMessage) ? 'show' : 'hide', "--err-msg"), className ? _xeUtils.default.isFunction(className) ? className(params) : className : '', itemClassName ? _xeUtils.default.isFunction(itemClassName) ? itemClassName(params) : itemClassName : '', (_ref = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_ref, "size--".concat(vSize), vSize), 'is--colon', titleColon), 'is--tbg', titleBackground), 'is--bold', titleBold), 'is--padding', itemPadding), 'is--vertical', itemVertical), 'is--asterisk', titleAsterisk), 'hide--content', showContent === false), 'is--valid', isValid), 'is--required', isRequired), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_ref, 'is--hidden', folding && collapseAll), 'is--active', (0, _util.isActiveItem)($xeForm, item)), 'err--icon', showErrorIcon), 'err--bg', showErrorBackground), 'is--error', showError))];
}
function getItemContentClass($xeForm, item, isGroup) {
  var formProps = $xeForm;
  var $xeGrid = $xeForm.$xeGrid;
  var data = formProps.data,
    readonly = formProps.readonly,
    disabled = formProps.disabled,
    allAlign = formProps.align,
    allVerticalAlign = formProps.verticalAlign;
  var field = item.field,
    itemRender = item.itemRender,
    contentClassName = item.contentClassName,
    children = item.children;
  var hasGroup = children && children.length > 0;
  var compConf = (0, _utils.isEnableConf)(itemRender) ? _ui.renderer.get(itemRender.name) : null;
  var itemContentClassName = compConf ? compConf.formItemContentClassName || compConf.itemContentClassName : '';
  var align = hasGroup ? item.align : _xeUtils.default.eqNull(item.align) ? allAlign : item.align;
  var verticalAlign = hasGroup ? item.verticalAlign : _xeUtils.default.eqNull(item.verticalAlign) ? allVerticalAlign : item.verticalAlign;
  var params = {
    data: data,
    disabled: disabled,
    readonly: readonly,
    field: field,
    property: field,
    item: item,
    $form: $xeForm,
    $grid: $xeGrid
  };
  return [isGroup || hasGroup ? 'vxe-form--group-content vxe-form--item-row' : '', 'vxe-form--item-content', align ? "align--".concat(align) : '', verticalAlign ? "vertical-align--".concat(verticalAlign) : '', itemContentClassName ? _xeUtils.default.isFunction(itemContentClassName) ? itemContentClassName(params) : itemContentClassName : '', contentClassName ? _xeUtils.default.isFunction(contentClassName) ? contentClassName(params) : contentClassName : ''];
}
function renderTitle(h, $xeForm, item, isGroup) {
  var formProps = $xeForm;
  var $xeGrid = $xeForm.$xeGrid;
  var data = formProps.data,
    readonly = formProps.readonly,
    disabled = formProps.disabled,
    allTitleAlign = formProps.titleAlign,
    allTitleWidth = formProps.titleWidth,
    allTitleOverflow = formProps.titleOverflow,
    allVertical = formProps.vertical;
  var slots = item.slots,
    title = item.title,
    field = item.field,
    itemRender = item.itemRender,
    titleOverflow = item.titleOverflow,
    vertical = item.vertical,
    showTitle = item.showTitle,
    titleClassName = item.titleClassName,
    titleStyle = item.titleStyle,
    titlePrefix = item.titlePrefix,
    titleSuffix = item.titleSuffix,
    children = item.children,
    showContent = item.showContent;
  var tooltipOpts = $xeForm.computeTooltipOpts;
  var compConf = (0, _utils.isEnableConf)(itemRender) ? _ui.renderer.get(itemRender.name) : null;
  var itemTitleClassName = compConf ? compConf.formItemTitleClassName || compConf.itemTitleClassName : '';
  var itemTitleStyle = compConf ? compConf.formItemTitleStyle || compConf.itemTitleStyle : null;
  var itemVertical = _xeUtils.default.eqNull(vertical) ? allVertical : vertical;
  var titleAlign = _xeUtils.default.eqNull(item.titleAlign) ? allTitleAlign : item.titleAlign;
  var titleWidth = itemVertical ? null : _xeUtils.default.eqNull(item.titleWidth) ? allTitleWidth : item.titleWidth;
  var itemOverflow = _xeUtils.default.eqNull(titleOverflow) ? allTitleOverflow : titleOverflow;
  var ovEllipsis = itemOverflow === 'ellipsis';
  var ovTitle = itemOverflow === 'title';
  var ovTooltip = itemOverflow === true || itemOverflow === 'tooltip';
  var hasEllipsis = ovTitle || ovTooltip || ovEllipsis;
  var params = {
    data: data,
    disabled: disabled,
    readonly: readonly,
    field: field,
    property: field,
    item: item,
    $form: $xeForm,
    $grid: $xeGrid
  };
  var titleSlot = slots ? slots.title : null;
  var prefixSlot = slots ? slots.prefix : null;
  var suffixSlot = slots ? slots.suffix || slots.extra : null;
  var isTitle = showTitle !== false && (title || titleSlot);
  var hasGroup = children && children.length > 0;
  var titVNs = [];
  if (prefixSlot) {
    titVNs.push(h('span', {
      key: 'pt',
      class: 'vxe-form--item-title-prefix'
    }, $xeForm.callSlot(prefixSlot, params, h)));
  }
  if (titlePrefix) {
    titVNs.push(titlePrefix.content || titlePrefix.message ? h(_tooltip.default, {
      key: 'pm',
      props: Object.assign(Object.assign(Object.assign({}, tooltipOpts), titlePrefix), {
        content: (0, _utils.getFuncText)(titlePrefix.content || titlePrefix.message)
      }),
      scopedSlots: {
        default: function _default() {
          return renderPrefixIcon(h, titlePrefix);
        }
      }
    }) : renderPrefixIcon(h, titlePrefix));
  }
  var rftTitle = compConf ? compConf.renderFormItemTitle || compConf.renderItemTitle : null;
  titVNs.push(h('span', {
    key: 'pl',
    class: 'vxe-form--item-title-label'
  }, titleSlot ? $xeForm.callSlot(titleSlot, params, h) : rftTitle ? (0, _vn.getSlotVNs)(rftTitle.call($xeForm, h, itemRender, params)) : (0, _utils.getFuncText)(item.title)));
  var fixVNs = [];
  if (titleSuffix) {
    fixVNs.push(titleSuffix.content || titleSuffix.message ? h(_tooltip.default, {
      key: 'sm',
      props: Object.assign(Object.assign(Object.assign({}, tooltipOpts), titleSuffix), {
        content: (0, _utils.getFuncText)(titleSuffix.content || titleSuffix.message)
      }),
      scopedSlots: {
        default: function _default() {
          return renderSuffixIcon(h, titleSuffix);
        }
      }
    }) : renderSuffixIcon(h, titleSuffix));
  }
  if (suffixSlot) {
    fixVNs.push(h('span', {
      key: 'st',
      class: 'vxe-form--item-title-suffix'
    }, $xeForm.callSlot(suffixSlot, params, h)));
  }
  var ons = ovTooltip ? {
    onMouseenter: function onMouseenter(evnt) {
      $xeForm.triggerTitleTipEvent(evnt, params);
    },
    onMouseleave: $xeForm.handleTitleTipLeaveEvent
  } : {};
  var itStyle = Object.assign({}, _xeUtils.default.isFunction(itemTitleStyle) ? itemTitleStyle(params) : itemTitleStyle, _xeUtils.default.isFunction(titleStyle) ? titleStyle(params) : titleStyle);
  if (titleWidth && titleWidth !== 'auto' && showContent !== false) {
    itStyle.width = (0, _dom.toCssUnit)(titleWidth);
  }
  return isTitle ? h('div', {
    class: [isGroup || hasGroup ? 'vxe-form--group-title' : '', 'vxe-form--item-title', titleAlign ? "align--".concat(titleAlign) : '', hasEllipsis ? 'is--ellipsis' : '', itemTitleClassName ? _xeUtils.default.isFunction(itemTitleClassName) ? itemTitleClassName(params) : itemTitleClassName : '', titleClassName ? _xeUtils.default.isFunction(titleClassName) ? titleClassName(params) : titleClassName : ''],
    style: itStyle,
    attrs: {
      itemid: item.id,
      title: ovTitle ? (0, _utils.getFuncText)(title) : null
    },
    on: ons
  }, [h('div', {
    class: 'vxe-form--item-title-content'
  }, titVNs), h('div', {
    class: 'vxe-form--item-title-postfix'
  }, fixVNs)]) : (0, _ui.renderEmptyElement)($xeForm);
}
function renderItemErrorIcon(h, $xeForm, item) {
  var validOpts = $xeForm.computeValidOpts;
  var showErrorIcon = validOpts.showErrorIcon,
    errorIcon = validOpts.errorIcon;
  var errRule = item.errRule,
    showIconMsg = item.showIconMsg;
  if (!showErrorIcon) {
    return (0, _ui.renderEmptyElement)($xeForm);
  }
  return h('div', {
    key: 'emi',
    class: ['vxe-form-item--valid-error-icon-wrapper', {
      'is--show': showIconMsg,
      'is--hide': !showIconMsg
    }]
  }, [h('span', {
    class: 'vxe-form-item--valid-error-icon-btn',
    on: {
      click: function click(evnt) {
        $xeForm.handleValidIconEvent(evnt, {
          item: item
        });
      }
    }
  }, [h('i', {
    class: errorIcon || (0, _ui.getIcon)().FORM_VALID_ERROR_ICON
  })]), h('div', {
    class: 'vxe-form-item--valid-error-icon-msg-tip'
  }, errRule ? [h('div', {
    class: "vxe-form-item--valid-error-icon-msg vxe-form-item--valid-error-icon-theme-".concat(validOpts.theme || 'normal')
  }, errRule.content || errRule.message)] : [])]);
}
function renderItemContent(h, $xeForm, item) {
  var formProps = $xeForm;
  var formReactData = $xeForm.reactData;
  var formInternalData = $xeForm.internalData;
  var $xeGrid = $xeForm.$xeGrid;
  var data = formProps.data,
    readonly = formProps.readonly,
    disabled = formProps.disabled;
  var itemFormatCache = formInternalData.itemFormatCache;
  var collapseAll = formReactData.collapseAll;
  var slots = item.slots,
    field = item.field,
    itemRender = item.itemRender,
    collapseNode = item.collapseNode,
    errRule = item.errRule,
    formatter = item.formatter;
  var defaultSlot = slots ? slots.default : null;
  var validSlot = slots ? slots.valid : null;
  var collapseOpts = $xeForm.computeCollapseOpts;
  var validOpts = $xeForm.computeValidOpts;
  var showErrorMessage = validOpts.showErrorMessage,
    showMessage = validOpts.showMessage;
  var compConf = (0, _utils.isEnableConf)(itemRender) ? _ui.renderer.get(itemRender.name) : null;
  var itemValue = _xeUtils.default.get(data, field);
  var params = {
    data: data,
    disabled: disabled,
    readonly: readonly,
    field: field,
    property: field,
    item: item,
    itemValue: itemValue,
    $form: $xeForm,
    $grid: $xeGrid
  };
  var contentVNs = [];
  var rftContent = compConf ? compConf.renderFormItemContent || compConf.renderItemContent : null;
  if (defaultSlot) {
    contentVNs = $xeForm.callSlot(defaultSlot, params, h);
  } else if (rftContent) {
    contentVNs = (0, _vn.getSlotVNs)(rftContent.call($xeForm, h, itemRender, params));
  } else if (field) {
    var itemLabel = itemValue;
    if (formatter) {
      var formatData;
      if (field) {
        var itemRest = itemFormatCache[field];
        if (itemRest) {
          formatData = itemRest.formatData;
          if (formatData) {
            if (formatData.value === itemValue) {
              return formatData.label;
            }
          } else {
            formatData = itemRest.formatData = {};
          }
        } else {
          itemFormatCache[field] = {
            field: field
          };
        }
      }
      if (_xeUtils.default.isString(formatter)) {
        var gFormatOpts = _ui.formats.get(formatter);
        var fiFormatMethod = gFormatOpts ? gFormatOpts.formItemFormatMethod : null;
        itemLabel = fiFormatMethod ? fiFormatMethod(params) : '';
      } else if (_xeUtils.default.isArray(formatter)) {
        var _gFormatOpts = _ui.formats.get(formatter[0]);
        var _fiFormatMethod = _gFormatOpts ? _gFormatOpts.formItemFormatMethod : null;
        itemLabel = _fiFormatMethod ? _fiFormatMethod.apply(void 0, [params].concat(_toConsumableArray(formatter.slice(1)))) : '';
      } else {
        itemLabel = formatter(params);
      }
      if (formatData) {
        formatData.value = itemValue;
        formatData.label = itemLabel;
      }
    }
    contentVNs = [(0, _utils.eqEmptyValue)(itemLabel) ? '' : "".concat(itemLabel)];
  }
  if (collapseNode) {
    contentVNs.push(h('div', {
      class: 'vxe-form--item-trigger-node',
      on: {
        click: $xeForm.toggleCollapseEvent
      }
    }, [h('span', {
      class: 'vxe-form--item-trigger-text'
    }, collapseAll ? collapseOpts.unfoldButtonText || (0, _ui.getI18n)('vxe.form.unfolding') : collapseOpts.foldButtonText || (0, _ui.getI18n)('vxe.form.folding')), h('i', {
      class: ['vxe-form--item-trigger-icon', collapseAll ? collapseOpts.foldIcon || (0, _ui.getIcon)().FORM_FOLDING : collapseOpts.unfoldIcon || (0, _ui.getIcon)().FORM_UNFOLDING]
    })]));
  }
  if (errRule && (_xeUtils.default.isBoolean(showErrorMessage) ? showErrorMessage : showMessage)) {
    var validParams = Object.assign(Object.assign({}, params), {
      rule: errRule
    });
    contentVNs.push(h('div', {
      class: 'vxe-form-item--valid-error-tip',
      style: errRule.maxWidth ? {
        width: (0, _dom.toCssUnit)(errRule.maxWidth)
      } : {}
    }, [h('div', {
      class: "vxe-form-item--valid-error-wrapper vxe-form-item--valid-error-theme-".concat(validOpts.theme || 'normal')
    }, [validSlot ? $xeForm.callSlot(validSlot, validParams, h) : [h('span', {
      class: 'vxe-form--item--valid-error-msg'
    }, errRule.content || errRule.message)]])]));
  }
  return h('div', {
    key: 'ct',
    class: 'vxe-form--item-inner'
  }, contentVNs);
}