"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = wrapPicker;

var _vue = _interopRequireWildcard(require("vue"));

var _Panel = _interopRequireDefault(require("../vc-time-picker/Panel"));

var _classNames3 = _interopRequireDefault(require("../_util/classNames"));

var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver"));

var _timePicker = require("../time-picker");

var _en_US = _interopRequireDefault(require("./locale/en_US"));

var _propsUtil = require("../_util/props-util");

var _configProvider = require("../config-provider");

var _momentUtil = require("../_util/moment-util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var DEFAULT_FORMAT = {
  date: 'YYYY-MM-DD',
  dateTime: 'YYYY-MM-DD HH:mm:ss',
  week: 'gggg-wo',
  month: 'YYYY-MM'
};
var LOCALE_FORMAT_MAPPING = {
  date: 'dateFormat',
  dateTime: 'dateTimeFormat',
  week: 'weekFormat',
  month: 'monthFormat'
};

function getColumns(_ref) {
  var showHour = _ref.showHour,
      showMinute = _ref.showMinute,
      showSecond = _ref.showSecond,
      use12Hours = _ref.use12Hours;
  var column = 0;

  if (showHour) {
    column += 1;
  }

  if (showMinute) {
    column += 1;
  }

  if (showSecond) {
    column += 1;
  }

  if (use12Hours) {
    column += 1;
  }

  return column;
}

function wrapPicker(Picker, props, pickerType) {
  return {
    name: Picker.name,
    inheritAttrs: false,
    props: (0, _propsUtil.initDefaultProps)(props, {
      transitionName: 'slide-up',
      popupStyle: {},
      locale: {}
    }),
    // model: {
    //   prop: 'value',
    //   event: 'change',
    // },
    setup: function setup() {
      return {
        configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
      };
    },
    created: function created() {
      (0, _vue.provide)('savePopupRef', this.savePopupRef);
    },
    mounted: function mounted() {
      var _this = this;

      var autofocus = this.autofocus,
          disabled = this.disabled,
          value = this.value,
          defaultValue = this.defaultValue,
          valueFormat = this.valueFormat;
      (0, _momentUtil.checkValidate)('DatePicker', defaultValue, 'defaultValue', valueFormat);
      (0, _momentUtil.checkValidate)('DatePicker', value, 'value', valueFormat);

      if (autofocus && !disabled) {
        this.$nextTick(function () {
          _this.focus();
        });
      }
    },
    watch: {
      value: function value(val) {
        (0, _momentUtil.checkValidate)('DatePicker', val, 'value', this.valueFormat);
      }
    },
    methods: {
      savePicker: function savePicker(node) {
        this.picker = node;
      },
      getDefaultLocale: function getDefaultLocale() {
        var result = _objectSpread(_objectSpread({}, _en_US.default), this.locale);

        result.lang = _objectSpread(_objectSpread({}, result.lang), (this.locale || {}).lang);
        return result;
      },
      savePopupRef: function savePopupRef(ref) {
        this.popupRef = ref;
      },
      handleOpenChange: function handleOpenChange(open) {
        this.$emit('openChange', open);
      },
      handleFocus: function handleFocus(e) {
        this.$emit('focus', e);
      },
      handleBlur: function handleBlur(e) {
        this.$emit('blur', e);
      },
      handleMouseEnter: function handleMouseEnter(e) {
        this.$emit('mouseenter', e);
      },
      handleMouseLeave: function handleMouseLeave(e) {
        this.$emit('mouseleave', e);
      },
      handleChange: function handleChange(date, dateString) {
        var value = this.valueFormat ? (0, _momentUtil.momentToString)(date, this.valueFormat) : date;
        this.$emit('update:value', value);
        this.$emit('change', value, dateString);
      },
      handleOk: function handleOk(val) {
        this.$emit('ok', this.valueFormat ? (0, _momentUtil.momentToString)(val, this.valueFormat) : val);
      },
      handleCalendarChange: function handleCalendarChange(date, dateString) {
        this.$emit('calendarChange', this.valueFormat ? (0, _momentUtil.momentToString)(date, this.valueFormat) : date, dateString);
      },
      focus: function focus() {
        this.picker.focus();
      },
      blur: function blur() {
        this.picker.blur();
      },
      transformValue: function transformValue(props) {
        if ('value' in props) {
          props.value = (0, _momentUtil.stringToMoment)(props.value, this.valueFormat);
        }

        if ('defaultValue' in props) {
          props.defaultValue = (0, _momentUtil.stringToMoment)(props.defaultValue, this.valueFormat);
        }

        if ('defaultPickerValue' in props) {
          props.defaultPickerValue = (0, _momentUtil.stringToMoment)(props.defaultPickerValue, this.valueFormat);
        }
      },
      renderPicker: function renderPicker(locale, localeCode) {
        var _classNames2;

        var props = _objectSpread(_objectSpread({}, (0, _propsUtil.getOptionProps)(this)), this.$attrs);

        this.transformValue(props);
        var customizePrefixCls = props.prefixCls,
            customizeInputPrefixCls = props.inputPrefixCls,
            getCalendarContainer = props.getCalendarContainer,
            size = props.size,
            showTime = props.showTime,
            disabled = props.disabled,
            format = props.format;
        var mergedPickerType = showTime ? "".concat(pickerType, "Time") : pickerType;
        var mergedFormat = format || locale[LOCALE_FORMAT_MAPPING[mergedPickerType]] || DEFAULT_FORMAT[mergedPickerType];
        var _this$configProvider = this.configProvider,
            getPrefixCls = _this$configProvider.getPrefixCls,
            getContextPopupContainer = _this$configProvider.getPopupContainer;
        var getPopupContainer = getCalendarContainer || getContextPopupContainer;
        var prefixCls = getPrefixCls('calendar', customizePrefixCls);
        var inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
        var pickerClass = (0, _classNames3.default)("".concat(prefixCls, "-picker"), _defineProperty({}, "".concat(prefixCls, "-picker-").concat(size), !!size));
        var pickerInputClass = (0, _classNames3.default)("".concat(prefixCls, "-picker-input"), inputPrefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(inputPrefixCls, "-lg"), size === 'large'), _defineProperty(_classNames2, "".concat(inputPrefixCls, "-sm"), size === 'small'), _defineProperty(_classNames2, "".concat(inputPrefixCls, "-disabled"), disabled), _classNames2));
        var timeFormat = showTime && showTime.format || 'HH:mm:ss';

        var vcTimePickerProps = _objectSpread(_objectSpread({}, (0, _timePicker.generateShowHourMinuteSecond)(timeFormat)), {}, {
          format: timeFormat,
          use12Hours: showTime && showTime.use12Hours
        });

        var columns = getColumns(vcTimePickerProps);
        var timePickerCls = "".concat(prefixCls, "-time-picker-column-").concat(columns);

        var timePickerPanelProps = _objectSpread(_objectSpread(_objectSpread({}, vcTimePickerProps), showTime), {}, {
          prefixCls: "".concat(prefixCls, "-time-picker"),
          placeholder: locale.timePickerLocale.placeholder,
          transitionName: 'slide-up',
          class: timePickerCls,
          onEsc: function onEsc() {}
        });

        var timePicker = showTime ? _vue.createVNode(_Panel.default, timePickerPanelProps, null) : null;

        var pickerProps = _objectSpread(_objectSpread({}, props), {}, {
          getCalendarContainer: getPopupContainer,
          format: mergedFormat,
          pickerClass: pickerClass,
          pickerInputClass: pickerInputClass,
          locale: locale,
          localeCode: localeCode,
          timePicker: timePicker,
          onOpenChange: this.handleOpenChange,
          onFocus: this.handleFocus,
          onBlur: this.handleBlur,
          onMouseenter: this.handleMouseEnter,
          onMouseleave: this.handleMouseLeave,
          onChange: this.handleChange,
          onOk: this.handleOk,
          onCalendarChange: this.handleCalendarChange,
          ref: this.savePicker
        });

        return _vue.createVNode(Picker, pickerProps, _objectSpread({}, this.$slots));
      }
    },
    render: function render() {
      return _vue.createVNode(_LocaleReceiver.default, {
        "componentName": "DatePicker",
        "defaultLocale": this.getDefaultLocale,
        "children": this.renderPicker
      }, null);
    }
  };
}