import { isArray, isUndefined } from 'lodash-es';
import { dateUtil } from '/nerv-lib/util/date-util';
const DATE_TYPE = [
  'NsDatePicker',
  'NsMonthPicker',
  'NsWeekPicker',
  'NsTimePicker',
  'NsRangePicker',
  'ADatePicker',
  'AMonthPicker',
  'AWeekPicker',
  'ATimePicker',
  'ARangePicker',
];

const INPUT_TYPE = ['NsInput', 'AInput', 'NsTextarea', 'ATextarea'];

/**
 * 是否时间组件
 */
export function isDateType(component: string | undefined) {
  if (isUndefined(component)) return false;
  return DATE_TYPE.includes(component);
}

/**
 * 是否输入框组件
 */
export function isInputType(component: string | undefined) {
  if (isUndefined(component)) return false;
  return INPUT_TYPE.includes(component);
}
/**
 *把时间转换为moment格式
 * @param component
 * @param value
 */
export const transformDate = (component: string, value: any, format?: any) => {
  if (value && isDateType(component)) {
    if (isArray(value)) {
      return value.map((item) => validTime(item, format));
    }
    return validTime(value, format);
  }
};

/**
 * 验证时间值和格式是否匹配 不匹配则格式化
 * @param time
 * @param format
 */
export function validTime(time: any, format: string) {
  if (dateUtil(time, format, true).isValid()) {
    return time;
  } else {
    return dateUtil(time).format(format);
  }
}
