function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useContext } from "react";
import ChevronDown from "@pisell/icon/es/ChevronDown";
import { Context } from "../model";
import { DatePicker } from "antd";
import dayjs from "dayjs";
import { getServiceTimes } from "../../utils";
import "./index.less";
var getLastService = function getLastService(list, index) {
  if (index === 0) {
    return null;
  }
  return list[index - 1];
};

// 更新服务的时间
export var updateServiceDate = function updateServiceDate(services, date, isTime) {
  var _list = [];
  for (var i = 0; i < services.length; i++) {
    var d = services[i];
    var _extend = _objectSpread(_objectSpread({}, d._extend), {}, {
      start_date: date
    });

    //  true表示 连服务的time都更新, 否则只更新date
    if (isTime) {
      var newDate = date;
      var newTime = date.format("HH:mm");
      // 获取上一个服务的时间, 如果是第一个则使用传递进来的时间, 否则使用上一个的结束时间
      var lastService = getLastService(_list, i);
      if (lastService) {
        newDate = lastService._extend.endDate;
        newTime = lastService._extend.endDate.format("HH:mm");
      }
      _extend = _objectSpread(_objectSpread({}, _extend), {}, {
        start_date: newDate,
        start_time: newTime
      });
    }
    var _getServiceTimes = getServiceTimes({
        _extend: _extend
      }),
      startDate = _getServiceTimes.startDate,
      endDate = _getServiceTimes.endDate;
    _list.push(_objectSpread(_objectSpread({}, d), {}, {
      _extend: _objectSpread(_objectSpread({}, _extend), {}, {
        startDate: startDate,
        endDate: endDate
      })
    }));
  }
  return _list;
};
var DateCom = function DateCom() {
  var modal = useContext(Context);

  // 禁用之前的日期
  var disabledDate = function disabledDate(current) {
    return current && current < dayjs().startOf("day");
  };
  var dateChange = function dateChange(e) {
    if (modal.state.disabledEdit) {
      return;
    }
    modal.dispatch({
      type: "setDate",
      payload: {
        value: e,
        edit: true
      }
    });
    setTimeout(function () {
      var _modal$state$service;
      if ((_modal$state$service = modal.state.service) !== null && _modal$state$service !== void 0 && (_modal$state$service = _modal$state$service.value) !== null && _modal$state$service !== void 0 && _modal$state$service.length) {
        var _modal$state$service2;
        var _list = updateServiceDate((_modal$state$service2 = modal.state.service) === null || _modal$state$service2 === void 0 ? void 0 : _modal$state$service2.value, e, false);
        modal.dispatch({
          type: "setService",
          payload: {
            value: _list,
            edit: true
          }
        });
      }
    }, 100);
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__booking-body-select-date"
  }, /*#__PURE__*/React.createElement(DatePicker, {
    inputReadOnly: true,
    allowClear: false,
    size: "large",
    style: {
      width: "100%",
      padding: 0
    },
    bordered: false,
    suffixIcon: !modal.state.disabledEdit ? /*#__PURE__*/React.createElement(ChevronDown, null) : null,
    disabledDate: disabledDate,
    format: modal.state.locale === "en" ? "dddd, DD MMM YYYY" : "YYYY年MM月DD日",
    value: modal.state.date.value,
    onChange: dateChange,
    disabled: modal.state.disabledEdit
  })));
};
export default DateCom;