"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _icons = require("@wii/icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _wauProvider = require("../wau-provider");
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const FieldLabel = props => {
  const {
    label,
    onClear,
    value,
    size = 'middle',
    disabled,
    ellipsis,
    placeholder,
    className,
    style,
    formatter,
    bordered,
    allowClear = true
  } = props;
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('pro-core-field-label');
  const intl = (0, _wauProvider.useIntl)();
  const getTextByValue = (aLabel, aValue) => {
    if (aValue !== undefined && aValue !== null && aValue !== '' && (!Array.isArray(aValue) || aValue.length)) {
      var _str;
      let str;
      if (formatter) {
        str = formatter(aValue);
      } else {
        str = Array.isArray(aValue) ? aValue.join(',') : String(aValue);
      }
      const prefix = aLabel ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [aLabel, ': ']
      }) : '';
      if (!ellipsis) {
        return /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
          children: [prefix, str]
        });
      }
      const getText = () => {
        const isArrayValue = Array.isArray(aValue) && aValue.length > 1;
        const unitText = intl.getMessage('form.lightFilter.itemUnit', '项');
        if (str.length > 32 && isArrayValue) {
          return `...${aValue.length}${unitText}`;
        }
        return '';
      };
      const tail = getText();
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
        title: str,
        children: [prefix, (_str = str) === null || _str === void 0 ? void 0 : _str.substr(0, 32), tail]
      });
    }
    return aLabel || placeholder;
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
    className: (0, _classnames.default)(prefixCls, `${prefixCls}-${size}`, {
      [`${prefixCls}-active`]: !!value || value === 0,
      [`${prefixCls}-disabled`]: disabled,
      [`${prefixCls}-bordered`]: bordered,
      [`${prefixCls}-allow-clear`]: allowClear
    }, className),
    style: style,
    children: [getTextByValue(label, value), (value || value === 0) && allowClear && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.CloseOutlined, {
      className: (0, _classnames.default)(`${prefixCls}-icon`, `${prefixCls}-close`),
      onClick: e => {
        if (onClear && !disabled) {
          onClear();
        }
        e.stopPropagation();
      }
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.DownOutlined, {
      className: (0, _classnames.default)(`${prefixCls}-icon`, `${prefixCls}-arrow`)
    })]
  });
};
var _default = FieldLabel;
exports.default = _default;