"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 = void 0;

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

var _moment = _interopRequireDefault(require("moment"));

var _vcCalendar = _interopRequireDefault(require("../vc-calendar"));

var _Picker = _interopRequireDefault(require("../vc-calendar/src/Picker"));

var _CloseCircleFilled = _interopRequireDefault(require("@ant-design/icons-vue/CloseCircleFilled"));

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

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

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

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _interface = require("./interface");

var _interopDefault = _interopRequireDefault(require("../_util/interopDefault"));

var _InputIcon = _interopRequireDefault(require("./InputIcon"));

var _util = require("../_util/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; }

function formatValue(value, format) {
  return value && value.format(format) || '';
}

function noop() {}

var _default = {
  name: 'AWeekPicker',
  mixins: [_BaseMixin.default],
  inheritAttrs: false,
  props: (0, _propsUtil.initDefaultProps)((0, _interface.WeekPickerProps)(), {
    format: 'gggg-wo',
    allowClear: true
  }),
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  data: function data() {
    var value = this.value || this.defaultValue;

    if (value && !(0, _interopDefault.default)(_moment.default).isMoment(value)) {
      throw new Error('The value/defaultValue of WeekPicker or MonthPicker must be ' + 'a moment object');
    }

    return {
      _value: value,
      _open: this.open
    };
  },
  watch: {
    value: function value(val) {
      var state = {
        _value: val
      };
      this.setState(state);
      this.prevState = _objectSpread(_objectSpread({}, this.$data), state);
    },
    open: function open(val) {
      var state = {
        _open: val
      };
      this.setState(state);
      this.prevState = _objectSpread(_objectSpread({}, this.$data), state);
    },
    _open: function _open(val, oldVal) {
      var _this = this;

      this.$nextTick(function () {
        if (!(0, _propsUtil.hasProp)(_this, 'open') && oldVal && !val) {
          _this.focus();
        }
      });
    }
  },
  mounted: function mounted() {
    this.prevState = _objectSpread({}, this.$data);
  },
  updated: function updated() {
    var _this2 = this;

    this.$nextTick(function () {
      if (!(0, _propsUtil.hasProp)(_this2, 'open') && _this2.prevState._open && !_this2._open) {
        _this2.focus();
      }
    });
  },
  methods: {
    saveInput: function saveInput(node) {
      this.input = node;
    },
    weekDateRender: function weekDateRender(_ref) {
      var current = _ref.current;
      var selectedValue = this.$data._value;
      var prefixCls = this._prefixCls,
          $slots = this.$slots;
      var dateRender = this.dateRender || $slots.dateRender;
      var dateNode = dateRender ? dateRender({
        current: current
      }) : current.date();

      if (selectedValue && current.year() === selectedValue.year() && current.week() === selectedValue.week()) {
        return _vue.createVNode("div", {
          "class": "".concat(prefixCls, "-selected-day")
        }, [_vue.createVNode("div", {
          "class": "".concat(prefixCls, "-date")
        }, [dateNode])]);
      }

      return _vue.createVNode("div", {
        "class": "".concat(prefixCls, "-date")
      }, [dateNode]);
    },
    handleChange: function handleChange(value) {
      if (!(0, _propsUtil.hasProp)(this, 'value')) {
        this.setState({
          _value: value
        });
      }

      this.$emit('change', value, formatValue(value, this.format));
    },
    handleOpenChange: function handleOpenChange(open) {
      if (!(0, _propsUtil.hasProp)(this, 'open')) {
        this.setState({
          _open: open
        });
      }

      this.$emit('openChange', open);
    },
    clearSelection: function clearSelection(e) {
      e.preventDefault();
      e.stopPropagation();
      this.handleChange(null);
    },
    focus: function focus() {
      this.input.focus();
    },
    blur: function blur() {
      this.input.blur();
    },
    renderFooter: function renderFooter() {
      var prefixCls = this._prefixCls,
          $slots = this.$slots;
      var renderExtraFooter = this.renderExtraFooter || $slots.renderExtraFooter;
      return renderExtraFooter ? _vue.createVNode("div", {
        "class": "".concat(prefixCls, "-footer-extra")
      }, [renderExtraFooter.apply(void 0, arguments)]) : null;
    }
  },
  render: function render() {
    var _this3 = this;

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

    var suffixIcon = (0, _propsUtil.getComponent)(this, 'suffixIcon');
    suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
    var customizePrefixCls = this.prefixCls,
        disabled = this.disabled,
        pickerClass = this.pickerClass,
        popupStyle = this.popupStyle,
        pickerInputClass = this.pickerInputClass,
        format = this.format,
        allowClear = this.allowClear,
        locale = this.locale,
        localeCode = this.localeCode,
        disabledDate = this.disabledDate,
        defaultPickerValue = this.defaultPickerValue,
        $data = this.$data,
        $slots = this.$slots;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('calendar', customizePrefixCls);
    this._prefixCls = prefixCls;
    var pickerValue = $data._value,
        open = $data._open;
    var className = props.class,
        style = props.style,
        id = props.id,
        _props$onFocus = props.onFocus,
        onFocus = _props$onFocus === void 0 ? noop : _props$onFocus,
        _props$onBlur = props.onBlur,
        onBlur = _props$onBlur === void 0 ? noop : _props$onBlur;

    if (pickerValue && localeCode) {
      pickerValue.locale(localeCode);
    }

    var placeholder = (0, _propsUtil.hasProp)(this, 'placeholder') ? this.placeholder : locale.lang.placeholder;
    var weekDateRender = this.dateRender || $slots.dateRender || this.weekDateRender;

    var calendar = _vue.createVNode(_vcCalendar.default, {
      "showWeekNumber": true,
      "dateRender": weekDateRender,
      "prefixCls": prefixCls,
      "format": format,
      "locale": locale.lang,
      "showDateInput": false,
      "showToday": false,
      "disabledDate": disabledDate,
      "renderFooter": this.renderFooter,
      "defaultValue": defaultPickerValue
    }, null);

    var clearIcon = !disabled && allowClear && $data._value ? _vue.createVNode(_CloseCircleFilled.default, {
      "class": "".concat(prefixCls, "-picker-clear"),
      "onClick": this.clearSelection
    }, null) : null;

    var inputIcon = _vue.createVNode(_InputIcon.default, {
      "suffixIcon": suffixIcon,
      "prefixCls": prefixCls
    }, null);

    var input = function input(_ref2) {
      var value = _ref2.value;
      return _vue.createVNode("span", {
        "style": {
          display: 'inline-block',
          width: '100%'
        }
      }, [_vue.createVNode("input", {
        "ref": _this3.saveInput,
        "disabled": disabled,
        "readonly": true,
        "value": value && value.format(format) || '',
        "placeholder": placeholder,
        "class": pickerInputClass,
        "onFocus": onFocus,
        "onBlur": onBlur
      }, null), clearIcon, inputIcon]);
    };

    var vcDatePickerProps = _objectSpread(_objectSpread({}, props), {}, {
      calendar: calendar,
      prefixCls: "".concat(prefixCls, "-picker-container"),
      value: pickerValue,
      open: open,
      onChange: this.handleChange,
      onOpenChange: this.handleOpenChange,
      style: popupStyle
    });

    return _vue.createVNode("span", _vue.mergeProps((0, _util.getDataAndAriaProps)(props), {
      "class": (0, _classNames.default)(className, pickerClass),
      "style": style,
      "id": id
    }), [_vue.createVNode(_Picker.default, vcDatePickerProps, _objectSpread({}, _objectSpread({
      default: input
    }, $slots)))]);
  }
};
exports.default = _default;