import React, { useContext } from 'react';
import { DownOutlined, CloseOutlined } from '@wii/icons';
import classNames from 'classnames';
import ConfigProvider from '../config-provider';
import { useIntl } from '../wau-provider';
import './style';
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
const FieldLabel = props => {
  const {
    label,
    onClear,
    value,
    size = 'middle',
    disabled,
    ellipsis,
    placeholder,
    className,
    style,
    formatter,
    bordered,
    allowClear = true
  } = props;
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('pro-core-field-label');
  const intl = 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__*/_jsxs(_Fragment, {
        children: [aLabel, ': ']
      }) : '';
      if (!ellipsis) {
        return /*#__PURE__*/_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__*/_jsxs("span", {
        title: str,
        children: [prefix, (_str = str) === null || _str === void 0 ? void 0 : _str.substr(0, 32), tail]
      });
    }
    return aLabel || placeholder;
  };
  return /*#__PURE__*/_jsxs("span", {
    className: classNames(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__*/_jsx(CloseOutlined, {
      className: classNames(`${prefixCls}-icon`, `${prefixCls}-close`),
      onClick: e => {
        if (onClear && !disabled) {
          onClear();
        }
        e.stopPropagation();
      }
    }), /*#__PURE__*/_jsx(DownOutlined, {
      className: classNames(`${prefixCls}-icon`, `${prefixCls}-arrow`)
    })]
  });
};
export default FieldLabel;