"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof3 = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _vue = require("vue");
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _request = _interopRequireDefault(require("../_util/request"));
var _datePicker = _interopRequireWildcard(require("../date-picker"));
var _props = require("../date-picker/generatePicker/props");
require("../vc-picker/RangePicker");
var _enums = require("./enums");
var _type = require("../_util/type");
require("dayjs");
var _classNames = _interopRequireDefault(require("../_util/classNames"));
var _useConfigInject2 = _interopRequireDefault(require("../_util/hooks/useConfigInject"));
var _form = require("../form");
var _FormItemContext = require("../form/FormItemContext");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof3(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; }
var rangeSinglePickerProps = function rangeSinglePickerProps() {
  return {
    pickerClassName: String // 分开选择的区间选择单个选择器样式
  };
};

var _commonProps = (0, _props.commonProps)();
var _datePickerProps = (0, _props.datePickerProps)();
var _rangePickerProps = (0, _props.rangePickerProps)();
var _rangeSinglePickerProps = rangeSinglePickerProps();
function datePickerProProps() {
  return (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, _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 (0, _request.default)('/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 = (0, _slicedToArray2.default)(_ref, 2),
    start = _ref2[0],
    end = _ref2[1];
  return !(start || end);
};
var DatePickerPro = (0, _vue.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 = (0, _useConfigInject2.default)('picker', props),
      prefixCls = _useConfigInject.prefixCls;
    var formItemContext = (0, _FormItemContext.useInjectFormItemContext)();
    var configLoading = (0, _vue.ref)(false);
    var config = (0, _vue.ref)();
    var cacheDates = (0, _vue.ref)();
    var rangeValue = (0, _vue.ref)();
    var hackValue = (0, _vue.ref)();
    var selectDefaultValue = (0, _vue.ref)();
    var handleConfig = function handleConfig(value) {
      var _props$onConfigChange;
      var formatData = _enums.DateFormatMap.findById(value === null || value === void 0 ? void 0 : value.formate);
      var _config = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, 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) === _enums.DateTypeEnum.Select) {
          selectDefaultValue.value = props.defaultValue;
        } else if ((value === null || value === void 0 ? void 0 : value.type) === _enums.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) === _enums.DateTypeEnum.Select) {
          var _config$value, _props$onChange;
          var defaultData = _enums.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) === _enums.DateTypeEnum.Span) {
          var _config$value2, _props$onChange2;
          var _defaultData = _enums.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);
        }
      }
    };
    (0, _vue.watch)([function () {
      return props.code;
    }, function () {
      return props.hisUuid;
    }, function () {
      return props.domain;
    }, function () {
      return props.config;
    }], function (_ref4, _ref5) {
      var _ref6 = (0, _slicedToArray2.default)(_ref4, 4),
        curCode = _ref6[0],
        curHisUuid = _ref6[1],
        curDomain = _ref6[2],
        curConfig = _ref6[3];
      var _ref7 = (0, _slicedToArray2.default)(_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 = (0, _vue.computed)(function () {
      return mergeProps((0, _objectSpread2.default)((0, _objectSpread2.default)({}, _commonProps), _rangePickerProps), props);
    });
    var rangeSingleMergedProps = (0, _vue.computed)(function () {
      var merged = mergeProps((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, _commonProps), _datePickerProps), _rangeSinglePickerProps), props);
      return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, merged), {}, {
        'onUpdate:value': undefined,
        defaultValue: undefined,
        showToday: props.showToday || false,
        showNow: props.showNow || false
      });
    });
    var rangeSingleMergedPickerClass = (0, _vue.computed)(function () {
      return (0, _classNames.default)("".concat(prefixCls.value, "-pro-range-single-picker"), props.pickerClassName);
    });
    (0, _vue.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 = (0, _slicedToArray2.default)(_ref8, 2),
        curValue = _ref9[0],
        curDefaultValue = _ref9[1];
      if (((_config$value3 = config.value) === null || _config$value3 === void 0 ? void 0 : _config$value3.type) === _enums.DateTypeEnum.Span) {
        rangeValue.value = curValue || curDefaultValue;
      }
    }, {
      immediate: true
    });
    var dateMergedProps = (0, _vue.computed)(function () {
      return mergeProps((0, _objectSpread2.default)((0, _objectSpread2.default)({}, _commonProps), _datePickerProps), props);
    });
    var renderDatePicker = function renderDatePicker() {
      var _config$value4, _config$value5, _config$value6;
      return (0, _vue.createVNode)(_datePicker.default, (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
        "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
      }), (0, _objectSpread2.default)({}, slots));
    };
    var handleRangeDisabledDate = function handleRangeDisabledDate(current) {
      var _DateSpanMap$findById, _config$value7;
      if (!cacheDates.value || cacheDates.value.length === 0) {
        return false;
      }
      var span = (_DateSpanMap$findById = _enums.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 = (0, _slicedToArray2.default)(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 (0, _vue.createVNode)(_datePicker.RangePicker, (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, 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
      }), (0, _objectSpread2.default)({}, 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 || (0, _typeof2.default)(end) !== 'object') {
        return false;
      }
      var span = (_DateSpanMap$findById2 = _enums.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 || (0, _typeof2.default)(start) !== 'object') {
        return false;
      }
      var span = (_DateSpanMap$findById3 = _enums.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 (0, _vue.createVNode)("div", (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attrs), {}, {
        "class": (0, _classNames.default)("".concat(prefixCls.value, "-pro-range-single"), attrs.class)
      }), [(0, _vue.createVNode)(_form.FormItemRest, null, {
        default: function _default() {
          return [(0, _vue.createVNode)(_datePicker.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({
            "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
          }), (0, _objectSpread2.default)({}, slots))];
        }
      }), (0, _vue.createVNode)("div", {
        "class": "".concat(prefixCls.value, "-pro-range-single-divider")
      }, null), (0, _vue.createVNode)(_form.FormItemRest, null, {
        default: function _default() {
          return [(0, _vue.createVNode)(_datePicker.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({
            "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
          }), (0, _objectSpread2.default)({}, slots))];
        }
      })]);
    };
    return function () {
      var _config$value21, _config$value22;
      if (((_config$value21 = config.value) === null || _config$value21 === void 0 ? void 0 : _config$value21.type) === _enums.DateTypeEnum.Select) {
        return renderDatePicker();
      } else if (((_config$value22 = config.value) === null || _config$value22 === void 0 ? void 0 : _config$value22.type) === _enums.DateTypeEnum.Span) {
        var _config$value23;
        return ((_config$value23 = config.value) === null || _config$value23 === void 0 ? void 0 : _config$value23.choice) === _enums.RangeTypeEnum.Single ? renderRangeSinglePicker() : renderRangePicker();
      } else {
        return null;
      }
    };
  }
});
var _default2 = (0, _type.withInstall)(DatePickerPro);
exports.default = _default2;