import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useContext } from 'react';
import Avatar from '../avatar';
import { pickProProps, omitUndefined } from '../utils';
import ConfigContext, { useIntl } from '../wau-provider';
import FieldPercent from '../wau-percent-field';
import FieldIndexColumn from '../wau-index-column-field';
import FieldProgress from '../wau-progress-field';
import FieldMoney from '../wau-money-field';
import FieldDatePicker from '../wau-date-picker-field';
import FieldFromNow from '../wau-from-now-field';
import FieldRangePicker from '../wau-range-picker-field';
import FieldCode from '../wau-code-field';
import FieldTimePicker from '../wau-time-picker-field';
import FieldText from '../wau-text-field';
import FieldTextArea from '../wau-text-area-field';
import FieldPassword from '../wau-password-field';
import FieldStatus from '../wau-status-field';
import FieldOptions from '../wau-options-field';
import FiledSelect, { proFieldParsingText, proFieldParsingValueEnumToArray } from '../wau-select-field';
import FiledCheckbox from '../wau-checkbox-field';
import FiledRate from '../wau-rate-field';
import FiledSwitch from '../wau-switch-field';
import FieldDigit from '../wau-digit-field';
import FieldSecond from '../wau-second-field';
import FieldRadio from '../wau-radio-field';
import FieldImage from '../wau-image-field';
import './style';

/** 默认的 Field 需要实现的功能 */

/** Value type by function */
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
/**
 * Render valueType object
 *
 * @param text String | number
 * @param valueType ProColumnsValueObjectType
 */
const defaultRenderTextByObject = (text, valueType, props) => {
  const pickFormItemProps = pickProProps(props.fieldProps);
  if (valueType.type === 'progress') {
    return /*#__PURE__*/_jsx(FieldProgress, {
      ...props,
      text: text,
      fieldProps: {
        status: valueType.status ? valueType.status : undefined,
        ...pickFormItemProps
      }
    });
  }
  if (valueType.type === 'money') {
    return /*#__PURE__*/_jsx(FieldMoney, {
      locale: valueType.locale,
      ...props,
      fieldProps: pickFormItemProps,
      text: text,
      moneySymbol: valueType.moneySymbol
    });
  }
  if (valueType.type === 'percent') {
    return /*#__PURE__*/_jsx(FieldPercent, {
      ...props,
      text: text,
      showSymbol: valueType.showSymbol,
      precision: valueType.precision,
      fieldProps: pickFormItemProps,
      showColor: valueType.showColor
    });
  }
  if (valueType.type === 'image') {
    return /*#__PURE__*/_jsx(FieldImage, {
      ...props,
      text: text,
      width: valueType.width
    });
  }
  return text;
};

/**
 * 根据不同的类型来转化数值
 *
 * @param text
 * @param valueType
 */
const defaultRenderText = (text, valueType, props, valueTypeMap) => {
  if (typeof valueType === 'object') {
    return defaultRenderTextByObject(text, valueType, props);
  }
  const {
    mode = 'read',
    emptyText = '-'
  } = props;
  const customValueTypeConfig = valueTypeMap && valueTypeMap[valueType];
  if (customValueTypeConfig) {
    // eslint-disable-next-line no-param-reassign
    delete props.ref;
    if (mode === 'read') {
      var _customValueTypeConfi;
      return (_customValueTypeConfi = customValueTypeConfig.render) === null || _customValueTypeConfi === void 0 ? void 0 : _customValueTypeConfi.call(customValueTypeConfig, text, {
        text: text,
        ...props,
        mode: mode || 'read'
      }, /*#__PURE__*/_jsx(_Fragment, {
        children: text
      }));
    }
    if (mode === 'update' || mode === 'edit') {
      var _customValueTypeConfi2;
      return (_customValueTypeConfi2 = customValueTypeConfig.renderFormItem) === null || _customValueTypeConfi2 === void 0 ? void 0 : _customValueTypeConfi2.call(customValueTypeConfig, text, {
        text: text,
        ...props
      }, /*#__PURE__*/_jsx(_Fragment, {
        children: text
      }));
    }
  }
  if (emptyText !== false && mode === 'read' && valueType !== 'option' && valueType !== 'switch') {
    if (typeof text !== 'boolean' && typeof text !== 'number' && !text) {
      const {
        fieldProps,
        render
      } = props;
      if (render) {
        return render(text, {
          mode,
          ...fieldProps
        }, /*#__PURE__*/_jsx(_Fragment, {
          children: emptyText
        }));
      }
      return /*#__PURE__*/_jsx(_Fragment, {
        children: emptyText
      });
    }
  }

  // eslint-disable-next-line no-param-reassign
  delete props.emptyText;

  /** 如果是金额的值 */
  if (valueType === 'money') {
    return /*#__PURE__*/_jsx(FieldMoney, {
      ...props,
      text: text
    });
  }

  /** 如果是日期的值 */
  if (valueType === 'date') {
    return /*#__PURE__*/_jsx(FieldDatePicker, {
      text: text,
      format: "YYYY-MM-DD",
      ...props
    });
  }

  /** 如果是周的值 */
  if (valueType === 'dateWeek') {
    return /*#__PURE__*/_jsx(FieldDatePicker, {
      text: text,
      format: "YYYY-wo",
      picker: "week",
      ...props
    });
  }

  /** 如果是月的值 */
  if (valueType === 'dateMonth') {
    return /*#__PURE__*/_jsx(FieldDatePicker, {
      text: text,
      format: "YYYY-MM",
      picker: "month",
      ...props
    });
  }

  /** 如果是季度的值 */
  if (valueType === 'dateQuarter') {
    return /*#__PURE__*/_jsx(FieldDatePicker, {
      text: text,
      format: "YYYY-\\QQ",
      picker: "quarter",
      ...props
    });
  }

  /** 如果是年的值 */
  if (valueType === 'dateYear') {
    return /*#__PURE__*/_jsx(FieldDatePicker, {
      text: text,
      format: "YYYY",
      picker: "year",
      ...props
    });
  }

  /** 如果是日期范围的值 */
  if (valueType === 'dateRange') {
    return /*#__PURE__*/_jsx(FieldRangePicker, {
      text: text,
      format: "YYYY-MM-DD",
      ...props
    });
  }

  /** 如果是日期加时间类型的值 */
  if (valueType === 'dateTime') {
    return /*#__PURE__*/_jsx(FieldDatePicker, {
      text: text,
      format: "YYYY-MM-DD HH:mm:ss",
      showTime: true,
      ...props
    });
  }

  /** 如果是日期加时间类型的值的值 */
  if (valueType === 'dateTimeRange') {
    // 值不存在的时候显示 "-"
    return /*#__PURE__*/_jsx(FieldRangePicker, {
      text: text,
      format: "YYYY-MM-DD HH:mm:ss",
      showTime: true,
      ...props
    });
  }

  /** 如果是时间类型的值 */
  if (valueType === 'time') {
    return /*#__PURE__*/_jsx(FieldTimePicker, {
      text: text,
      format: "HH:mm:ss",
      ...props
    });
  }
  if (valueType === 'fromNow') {
    return /*#__PURE__*/_jsx(FieldFromNow, {
      text: text,
      ...props
    });
  }
  if (valueType === 'index') {
    return /*#__PURE__*/_jsx(FieldIndexColumn, {
      children: text + 1
    });
  }
  if (valueType === 'indexBorder') {
    return /*#__PURE__*/_jsx(FieldIndexColumn, {
      border: true,
      children: text + 1
    });
  }
  if (valueType === 'progress') {
    return /*#__PURE__*/_jsx(FieldProgress, {
      ...props,
      text: text
    });
  }
  /** 百分比, 默认展示符号, 不展示小数位 */
  if (valueType === 'percent') {
    return /*#__PURE__*/_jsx(FieldPercent, {
      text: text,
      ...props
    });
  }
  if (valueType === 'avatar' && typeof text === 'string' && props.mode === 'read') {
    return /*#__PURE__*/_jsx(Avatar, {
      src: text,
      size: 22,
      shape: "circle"
    });
  }
  if (valueType === 'code') {
    return /*#__PURE__*/_jsx(FieldCode, {
      text: text,
      ...props
    });
  }
  if (valueType === 'jsonCode') {
    return /*#__PURE__*/_jsx(FieldCode, {
      text: text,
      language: "json",
      ...props
    });
  }
  if (valueType === 'textarea') {
    return /*#__PURE__*/_jsx(FieldTextArea, {
      text: text,
      ...props
    });
  }
  if (valueType === 'digit') {
    return /*#__PURE__*/_jsx(FieldDigit, {
      text: text,
      ...props
    });
  }
  if (valueType === 'second') {
    return /*#__PURE__*/_jsx(FieldSecond, {
      text: text,
      ...props
    });
  }
  if (valueType === 'select' || valueType === 'text' && (props.valueEnum || props.request)) {
    return /*#__PURE__*/_jsx(FiledSelect, {
      text: text,
      ...props
    });
  }
  if (valueType === 'checkbox') {
    return /*#__PURE__*/_jsx(FiledCheckbox, {
      text: text,
      ...props
    });
  }
  if (valueType === 'radio') {
    return /*#__PURE__*/_jsx(FieldRadio, {
      text: text,
      ...props
    });
  }
  if (valueType === 'radioButton') {
    return /*#__PURE__*/_jsx(FieldRadio, {
      radioType: "button",
      text: text,
      ...props
    });
  }
  if (valueType === 'rate') {
    return /*#__PURE__*/_jsx(FiledRate, {
      text: text,
      ...props
    });
  }
  if (valueType === 'switch') {
    return /*#__PURE__*/_jsx(FiledSwitch, {
      text: text,
      ...props
    });
  }
  if (valueType === 'option') {
    return /*#__PURE__*/_jsx(FieldOptions, {
      text: text,
      ...props
    });
  }
  if (valueType === 'password') {
    return /*#__PURE__*/_jsx(FieldPassword, {
      text: text,
      ...props
    });
  }
  if (valueType === 'image') {
    return /*#__PURE__*/_jsx(FieldImage, {
      text: text,
      ...props
    });
  }
  return /*#__PURE__*/_jsx(FieldText, {
    text: text,
    ...props
  });
};
export { defaultRenderText };

/** ProField 的类型 */

const ProField = ({
  text,
  valueType = 'text',
  onChange,
  value,
  ...rest
}, ref) => {
  var _ref;
  const intl = useIntl();
  const context = useContext(ConfigContext);
  const fieldProps = (value || onChange || (rest === null || rest === void 0 ? void 0 : rest.fieldProps)) && {
    value,
    // fieldProps 优先级更高，在类似 LightFilter 场景下需要覆盖默认的 value 和 onChange
    ...omitUndefined(rest === null || rest === void 0 ? void 0 : rest.fieldProps),
    onChange: (...restParams) => {
      var _rest$fieldProps, _rest$fieldProps$onCh;
      onChange === null || onChange === void 0 ? void 0 : onChange(...restParams);
      rest === null || rest === void 0 ? void 0 : (_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 ? void 0 : (_rest$fieldProps$onCh = _rest$fieldProps.onChange) === null || _rest$fieldProps$onCh === void 0 ? void 0 : _rest$fieldProps$onCh.call(_rest$fieldProps, ...restParams);
    }
  };
  return /*#__PURE__*/_jsx(_Fragment, {
    children: defaultRenderText((_ref = text !== null && text !== void 0 ? text : fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.value) !== null && _ref !== void 0 ? _ref : '', valueType || 'text', {
      ...rest,
      mode: rest.mode || 'read',
      ref,
      placeholder: intl.getMessage('tableForm.inputPlaceholder', '请输入'),
      fieldProps: pickProProps(fieldProps)
    }, context.valueTypeMap)
  });
};
export { FieldPercent, FieldIndexColumn, FieldProgress, FieldMoney, FieldDatePicker, FieldRangePicker, FieldCode, FieldTimePicker, FieldText, FieldStatus, FiledSelect, proFieldParsingText, proFieldParsingValueEnumToArray };
export default /*#__PURE__*/React.forwardRef(ProField);