import _typeof from "@babel/runtime/helpers/esm/typeof";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createVNode as _createVNode, resolveDirective as _resolveDirective } from "vue";
import request from '../_util/request';
import { computed, defineComponent, ref, watch } from 'vue';
import DatePicker, { RangePicker } from '../date-picker';
import { commonProps, rangePickerProps, datePickerProps } from '../date-picker/generatePicker/props';
import '../vc-picker/RangePicker';
import { DateFormatMap, DateSpanMap, DateTypeEnum, DateDefaultMap, DateRangeDefaultMap, RangeTypeEnum } from './enums';
import { withInstall } from '../_util/type';
import 'dayjs';
import classNames from '../_util/classNames';
import useConfigInject from '../_util/hooks/useConfigInject';
import { FormItemRest } from '../form';
import { useInjectFormItemContext } from '../form/FormItemContext';
var rangeSinglePickerProps = function rangeSinglePickerProps() {
  return {
    pickerClassName: String // 分开选择的区间选择单个选择器样式
  };
};

var _commonProps = commonProps();
var _datePickerProps = datePickerProps();
var _rangePickerProps = rangePickerProps();
var _rangeSinglePickerProps = rangeSinglePickerProps();
function datePickerProProps() {
  return _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, _commonProps), _datePickerProps), _rangePickerProps), _rangeSinglePickerProps), {}, {
    defaultPickerValue: {
      type: Array
    },
    defaultValue: {
      type: Array
    },
    value: {
      type: Array
    },
    disabledTime: {
      type: [Function, Function]
    },
    format: _datePickerProps.format,
    renderExtraFooter: {
      type: [Function, Function]
    },
    onChange: {
      type: [Function, Function]
    },
    'onUpdate:value': {
      type: [Function, Function]
    },
    onOk: {
      type: [Function, Function]
    },
    code: String,
    hisUuid: String,
    domain: String,
    config: Object,
    onConfigChange: Function,
    'onUpdate:configValue': Function
  });
}
var getCofig = function getCofig(params) {
  return request('/develop/module/time/render.open', {
    params: params
  });
};
var mergeProps = function mergeProps(originProps, props) {
  var res = {};
  Object.keys(originProps).forEach(function (key) {
    if (props.hasOwnProperty(key) && props[key] !== undefined) {
      res[key] = props[key];
    }
  });
  return res;
};
var isEmptyRange = function isEmptyRange(value) {
  if (!(value !== null && value !== void 0 && value.length)) {
    return true;
  }
  var _ref = value || [],
    _ref2 = _slicedToArray(_ref, 2),
    start = _ref2[0],
    end = _ref2[1];
  return !(start || end);
};
var DatePickerPro = defineComponent({
  name: 'PfDatePickerPro',
  inheritAttrs: false,
  props: datePickerProProps(),
  slots: ['suffixIcon', 'prevIcon', 'nextIcon', 'superPrevIcon', 'superNextIcon', 'dateRender', 'renderExtraFooter', 'monthCellRender'],
  setup: function setup(props, _ref3) {
    var attrs = _ref3.attrs,
      slots = _ref3.slots,
      emit = _ref3.emit;
    var _useConfigInject = useConfigInject('picker', props),
      prefixCls = _useConfigInject.prefixCls;
    var formItemContext = useInjectFormItemContext();
    var configLoading = ref(false);
    var config = ref();
    var cacheDates = ref();
    var rangeValue = ref();
    var hackValue = ref();
    var selectDefaultValue = ref();
    var handleConfig = function handleConfig(value) {
      var _props$onConfigChange;
      var formatData = DateFormatMap.findById(value === null || value === void 0 ? void 0 : value.formate);
      var _config = _objectSpread(_objectSpread({}, value), {}, {
        picker: formatData === null || formatData === void 0 ? void 0 : formatData.picker,
        showTime: formatData === null || formatData === void 0 ? void 0 : formatData.showTime,
        format: formatData === null || formatData === void 0 ? void 0 : formatData.name
      });
      config.value = _config;
      (_props$onConfigChange = props.onConfigChange) === null || _props$onConfigChange === void 0 ? void 0 : _props$onConfigChange.call(props, _config);
      emit('update:configValue', _config);
      if (props.value) {
        return;
      }
      if (props.defaultValue) {
        if ((value === null || value === void 0 ? void 0 : value.type) === DateTypeEnum.Select) {
          selectDefaultValue.value = props.defaultValue;
        } else if ((value === null || value === void 0 ? void 0 : value.type) === DateTypeEnum.Span) {
          rangeValue.value = props.defaultValue;
        }
      } else if (value !== null && value !== void 0 && value.defaultVal) {
        if ((value === null || value === void 0 ? void 0 : value.type) === DateTypeEnum.Select) {
          var _config$value, _props$onChange;
          var defaultData = DateDefaultMap.findById((_config$value = config.value) === null || _config$value === void 0 ? void 0 : _config$value.defaultVal);
          var defaultValue = defaultData && defaultData.getDefaultValue && defaultData.getDefaultValue();
          selectDefaultValue.value = defaultValue;
          (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, defaultValue, defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.format(formatData === null || formatData === void 0 ? void 0 : formatData.name));
          emit('update:value', defaultValue);
        } else if ((value === null || value === void 0 ? void 0 : value.type) === DateTypeEnum.Span) {
          var _config$value2, _props$onChange2;
          var _defaultData = DateRangeDefaultMap.findById((_config$value2 = config.value) === null || _config$value2 === void 0 ? void 0 : _config$value2.defaultVal);
          var _defaultValue2 = _defaultData && _defaultData.getDefaultValue && _defaultData.getDefaultValue();
          var valueString = _defaultValue2 === null || _defaultValue2 === void 0 ? void 0 : _defaultValue2.map(function (v) {
            return v === null || v === void 0 ? void 0 : v.format(formatData === null || formatData === void 0 ? void 0 : formatData.name);
          });
          var _defaultValue = isEmptyRange(_defaultValue2) ? null : _defaultValue2;
          rangeValue.value = _defaultValue;
          (_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 ? void 0 : _props$onChange2.call(props, _defaultValue, valueString);
          emit('update:value', _defaultValue);
        }
      }
    };
    watch([function () {
      return props.code;
    }, function () {
      return props.hisUuid;
    }, function () {
      return props.domain;
    }, function () {
      return props.config;
    }], function (_ref4, _ref5) {
      var _ref6 = _slicedToArray(_ref4, 4),
        curCode = _ref6[0],
        curHisUuid = _ref6[1],
        curDomain = _ref6[2],
        curConfig = _ref6[3];
      var _ref7 = _slicedToArray(_ref5, 4),
        preCode = _ref7[0],
        preHisUuid = _ref7[1],
        preDomain = _ref7[2],
        preConfig = _ref7[3];
      if (curCode && curCode !== preCode || curHisUuid && curHisUuid !== preHisUuid || (curCode || curHisUuid) && curDomain !== preDomain) {
        configLoading.value = true;
        getCofig({
          moduleCode: curCode || '',
          hisUuid: curHisUuid && !curCode ? curHisUuid : ''
        }).then(function (res) {
          handleConfig(res);
        }).finally(function () {
          configLoading.value = false;
        });
      } else if (curConfig && curConfig !== preConfig) {
        Promise.resolve().then(function () {
          handleConfig(curConfig);
        });
      }
    }, {
      immediate: true
    });
    var rangeMergedProps = computed(function () {
      return mergeProps(_objectSpread(_objectSpread({}, _commonProps), _rangePickerProps), props);
    });
    var rangeSingleMergedProps = computed(function () {
      var merged = mergeProps(_objectSpread(_objectSpread(_objectSpread({}, _commonProps), _datePickerProps), _rangeSinglePickerProps), props);
      return _objectSpread(_objectSpread({}, merged), {}, {
        'onUpdate:value': undefined,
        defaultValue: undefined,
        showToday: props.showToday || false,
        showNow: props.showNow || false
      });
    });
    var rangeSingleMergedPickerClass = computed(function () {
      return classNames("".concat(prefixCls.value, "-pro-range-single-picker"), props.pickerClassName);
    });
    watch([function () {
      return props.value;
    }, function () {
      var _rangeMergedProps$val;
      return (_rangeMergedProps$val = rangeMergedProps.value) === null || _rangeMergedProps$val === void 0 ? void 0 : _rangeMergedProps$val.defaultValue;
    }], function (_ref8) {
      var _config$value3;
      var _ref9 = _slicedToArray(_ref8, 2),
        curValue = _ref9[0],
        curDefaultValue = _ref9[1];
      if (((_config$value3 = config.value) === null || _config$value3 === void 0 ? void 0 : _config$value3.type) === DateTypeEnum.Span) {
        rangeValue.value = curValue || curDefaultValue;
      }
    }, {
      immediate: true
    });
    var dateMergedProps = computed(function () {
      return mergeProps(_objectSpread(_objectSpread({}, _commonProps), _datePickerProps), props);
    });
    var renderDatePicker = function renderDatePicker() {
      var _config$value4, _config$value5, _config$value6;
      return _createVNode(DatePicker, _objectSpread(_objectSpread(_objectSpread({
        "placeholder": "请选择",
        "defaultValue": selectDefaultValue.value
      }, attrs), dateMergedProps.value), {}, {
        "picker": (_config$value4 = config.value) === null || _config$value4 === void 0 ? void 0 : _config$value4.picker,
        "showTime": ((_config$value5 = config.value) === null || _config$value5 === void 0 ? void 0 : _config$value5.showTime) && {
          format: config.value.showTime
        },
        "format": (_config$value6 = config.value) === null || _config$value6 === void 0 ? void 0 : _config$value6.format
      }), _objectSpread({}, slots));
    };
    var handleRangeDisabledDate = function handleRangeDisabledDate(current) {
      var _DateSpanMap$findById, _config$value7;
      if (!cacheDates.value || cacheDates.value.length === 0) {
        return false;
      }
      var span = (_DateSpanMap$findById = DateSpanMap.findById((_config$value7 = config.value) === null || _config$value7 === void 0 ? void 0 : _config$value7.span)) === null || _DateSpanMap$findById === void 0 ? void 0 : _DateSpanMap$findById.span;
      var _cacheDates$value = _slicedToArray(cacheDates.value, 2),
        start = _cacheDates$value[0],
        end = _cacheDates$value[1];
      var tooLate = start && current.diff(start, 'days') > span;
      var tooEarly = end && end.diff(current, 'days') > span;
      return tooEarly || tooLate;
    };
    var handleRangeOpenChange = function handleRangeOpenChange(open) {
      var _rangeMergedProps$val2, _rangeMergedProps$val3;
      if (open) {
        cacheDates.value = [];
        hackValue.value = [];
      } else {
        hackValue.value = undefined;
      }
      (_rangeMergedProps$val2 = rangeMergedProps.value) === null || _rangeMergedProps$val2 === void 0 ? void 0 : (_rangeMergedProps$val3 = _rangeMergedProps$val2.onOpenChange) === null || _rangeMergedProps$val3 === void 0 ? void 0 : _rangeMergedProps$val3.call(_rangeMergedProps$val2, open);
    };
    var handleRangeChange = function handleRangeChange(value, dateString) {
      var _rangeMergedProps$val4, _rangeMergedProps$val5;
      rangeValue.value = value;
      (_rangeMergedProps$val4 = rangeMergedProps.value) === null || _rangeMergedProps$val4 === void 0 ? void 0 : (_rangeMergedProps$val5 = _rangeMergedProps$val4.onChange) === null || _rangeMergedProps$val5 === void 0 ? void 0 : _rangeMergedProps$val5.call(_rangeMergedProps$val4, value, dateString);
    };
    var handleRangeCalendarChange = function handleRangeCalendarChange(value, formatString, info) {
      var _rangeMergedProps$val6, _rangeMergedProps$val7;
      cacheDates.value = value;
      (_rangeMergedProps$val6 = rangeMergedProps.value) === null || _rangeMergedProps$val6 === void 0 ? void 0 : (_rangeMergedProps$val7 = _rangeMergedProps$val6.onCalendarChange) === null || _rangeMergedProps$val7 === void 0 ? void 0 : _rangeMergedProps$val7.call(_rangeMergedProps$val6, value, formatString, info);
    };
    var renderRangePicker = function renderRangePicker() {
      var _config$value8, _config$value9, _config$value10;
      return _createVNode(RangePicker, _objectSpread(_objectSpread(_objectSpread({}, attrs), rangeMergedProps.value), {}, {
        "picker": (_config$value8 = config.value) === null || _config$value8 === void 0 ? void 0 : _config$value8.picker,
        "showTime": ((_config$value9 = config.value) === null || _config$value9 === void 0 ? void 0 : _config$value9.showTime) && {
          format: config.value.showTime
        },
        "format": (_config$value10 = config.value) === null || _config$value10 === void 0 ? void 0 : _config$value10.format,
        "disabledDate": handleRangeDisabledDate,
        "onChange": handleRangeChange,
        "onOpenChange": handleRangeOpenChange,
        "onCalendarChange": handleRangeCalendarChange,
        "value": hackValue.value || props.value || rangeValue.value
      }), _objectSpread({}, slots));
    };
    var handleRangeSingleStartChange = function handleRangeSingleStartChange(value) {
      var _rangeValue$value, _rangeSingleMergedPro, _rangeSingleMergedPro2;
      var range = [value || undefined, (_rangeValue$value = rangeValue.value) === null || _rangeValue$value === void 0 ? void 0 : _rangeValue$value[1]];
      var dateString = range.map(function (v) {
        var _config$value11;
        return v === null || v === void 0 ? void 0 : v.format((_config$value11 = config.value) === null || _config$value11 === void 0 ? void 0 : _config$value11.format);
      });
      var _range = isEmptyRange(range) ? null : range;
      rangeValue.value = _range;
      (_rangeSingleMergedPro = (_rangeSingleMergedPro2 = rangeSingleMergedProps.value).onChange) === null || _rangeSingleMergedPro === void 0 ? void 0 : _rangeSingleMergedPro.call(_rangeSingleMergedPro2, _range, dateString);
      emit('update:value', _range);
      formItemContext.onFieldChange();
    };
    var handleRangeSingleEndChange = function handleRangeSingleEndChange(value) {
      var _rangeValue$value2, _rangeSingleMergedPro3, _rangeSingleMergedPro4;
      var range = [(_rangeValue$value2 = rangeValue.value) === null || _rangeValue$value2 === void 0 ? void 0 : _rangeValue$value2[0], value || undefined];
      var dateString = range.map(function (v) {
        var _config$value12;
        return v === null || v === void 0 ? void 0 : v.format((_config$value12 = config.value) === null || _config$value12 === void 0 ? void 0 : _config$value12.format);
      });
      var _range = isEmptyRange(range) ? null : range;
      rangeValue.value = _range;
      (_rangeSingleMergedPro3 = (_rangeSingleMergedPro4 = rangeSingleMergedProps.value).onChange) === null || _rangeSingleMergedPro3 === void 0 ? void 0 : _rangeSingleMergedPro3.call(_rangeSingleMergedPro4, _range, dateString);
      emit('update:value', _range);
      formItemContext.onFieldChange();
    };
    var handleRangeSingleBlur = function handleRangeSingleBlur() {
      emit('blur');
      formItemContext.onFieldBlur();
    };
    var handleRangeSingleStartDisabledDate = function handleRangeSingleStartDisabledDate(current) {
      var _rangeValue$value3, _DateSpanMap$findById2, _config$value13;
      var end = (_rangeValue$value3 = rangeValue.value) === null || _rangeValue$value3 === void 0 ? void 0 : _rangeValue$value3[1];
      if (!end || _typeof(end) !== 'object') {
        return false;
      }
      var span = (_DateSpanMap$findById2 = DateSpanMap.findById((_config$value13 = config.value) === null || _config$value13 === void 0 ? void 0 : _config$value13.span)) === null || _DateSpanMap$findById2 === void 0 ? void 0 : _DateSpanMap$findById2.span;
      var tooLate = current.isAfter(end);
      var tooEarly = end.diff(current, 'days') > span;
      return tooEarly || tooLate;
    };
    var handleRangeSingleEndDisabledDate = function handleRangeSingleEndDisabledDate(current) {
      var _rangeValue$value4, _DateSpanMap$findById3, _config$value14;
      var start = (_rangeValue$value4 = rangeValue.value) === null || _rangeValue$value4 === void 0 ? void 0 : _rangeValue$value4[0];
      if (!start || _typeof(start) !== 'object') {
        return false;
      }
      var span = (_DateSpanMap$findById3 = DateSpanMap.findById((_config$value14 = config.value) === null || _config$value14 === void 0 ? void 0 : _config$value14.span)) === null || _DateSpanMap$findById3 === void 0 ? void 0 : _DateSpanMap$findById3.span;
      var tooLate = current.diff(start, 'days') > span;
      var tooEarly = current.isBefore(start);
      return tooEarly || tooLate;
    };
    var renderRangeSinglePicker = function renderRangeSinglePicker() {
      var _rangeValue$value5, _config$value15, _config$value16, _config$value17, _rangeValue$value6, _config$value18, _config$value19, _config$value20;
      return _createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
        "class": classNames("".concat(prefixCls.value, "-pro-range-single"), attrs.class)
      }), [_createVNode(FormItemRest, null, {
        default: function _default() {
          return [_createVNode(DatePicker, _objectSpread(_objectSpread({
            "placeholder": "请选择"
          }, rangeSingleMergedProps.value), {}, {
            "value": (_rangeValue$value5 = rangeValue.value) === null || _rangeValue$value5 === void 0 ? void 0 : _rangeValue$value5[0],
            "onChange": handleRangeSingleStartChange,
            "onBlur": handleRangeSingleBlur,
            "picker": (_config$value15 = config.value) === null || _config$value15 === void 0 ? void 0 : _config$value15.picker,
            "showTime": ((_config$value16 = config.value) === null || _config$value16 === void 0 ? void 0 : _config$value16.showTime) && {
              format: config.value.showTime
            },
            "format": (_config$value17 = config.value) === null || _config$value17 === void 0 ? void 0 : _config$value17.format,
            "disabledDate": handleRangeSingleStartDisabledDate,
            "class": rangeSingleMergedPickerClass.value
          }), _objectSpread({}, slots))];
        }
      }), _createVNode("div", {
        "class": "".concat(prefixCls.value, "-pro-range-single-divider")
      }, null), _createVNode(FormItemRest, null, {
        default: function _default() {
          return [_createVNode(DatePicker, _objectSpread(_objectSpread({
            "placeholder": "请选择"
          }, rangeSingleMergedProps.value), {}, {
            "value": (_rangeValue$value6 = rangeValue.value) === null || _rangeValue$value6 === void 0 ? void 0 : _rangeValue$value6[1],
            "onChange": handleRangeSingleEndChange,
            "onBlur": handleRangeSingleBlur,
            "picker": (_config$value18 = config.value) === null || _config$value18 === void 0 ? void 0 : _config$value18.picker,
            "showTime": ((_config$value19 = config.value) === null || _config$value19 === void 0 ? void 0 : _config$value19.showTime) && {
              format: config.value.showTime
            },
            "format": (_config$value20 = config.value) === null || _config$value20 === void 0 ? void 0 : _config$value20.format,
            "disabledDate": handleRangeSingleEndDisabledDate,
            "class": rangeSingleMergedPickerClass.value
          }), _objectSpread({}, slots))];
        }
      })]);
    };
    return function () {
      var _config$value21, _config$value22;
      if (((_config$value21 = config.value) === null || _config$value21 === void 0 ? void 0 : _config$value21.type) === DateTypeEnum.Select) {
        return renderDatePicker();
      } else if (((_config$value22 = config.value) === null || _config$value22 === void 0 ? void 0 : _config$value22.type) === DateTypeEnum.Span) {
        var _config$value23;
        return ((_config$value23 = config.value) === null || _config$value23 === void 0 ? void 0 : _config$value23.choice) === RangeTypeEnum.Single ? renderRangeSinglePicker() : renderRangePicker();
      } else {
        return null;
      }
    };
  }
});
export default withInstall(DatePickerPro);