'use strict';

var core = require('@linaria/core');
var index$1 = require('../../core/Button/index.js');
var index = require('../../core/Typo/index.js');
var Calendar = require('./Calendar.js');
var InputTime = require('./InputTime.js');
var useMemoCompare = require('../../hooks/useMemoCompare.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var dayjs = require('dayjs');
var _ = require('lodash');
var React = require('react');
var common = require('./common.js');
var DateRangePicker_style = require('./DateRangePicker.style.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var dayjs__default = /*#__PURE__*/_interopDefault(dayjs);
var React__default = /*#__PURE__*/_interopDefault(React);

function transformTime(type, day, boundaryDate) {
  if (day) {
    const _boundaryDate = dayjs__default.default(boundaryDate);
    if (type === "start" && _boundaryDate.isValid()) {
      const start = day.isBefore(_boundaryDate) ? _boundaryDate : day;
      return [
        common.time2string(start.hour()),
        common.time2string(start.minute()),
        common.time2string(start.second())
      ];
    }
    if (type === "end" && _boundaryDate.isValid()) {
      const end = day.isAfter(_boundaryDate) ? _boundaryDate : day;
      return [
        common.time2string(end.hour()),
        common.time2string(end.minute()),
        common.time2string(end.second())
      ];
    }
  }
  return ["", "", ""];
}
function updateDate(time, date) {
  const [hour, minute, second] = time;
  return common.copyDay(dayjs__default.default(date == null ? void 0 : date.format())).set("hour", parseInt(hour || "0")).set("minute", parseInt(minute || "0")).set("second", parseInt(second || "0"));
}
function checkMinAndMaxTimeRange(t, range, timeRange, minDate, maxDate) {
  const [startDate, endDate] = range;
  const [startTime, endTime] = timeRange;
  if (!startDate && !endDate) {
    return void 0;
  }
  if (!minDate && !maxDate) {
    return void 0;
  }
  const _minDate = common.copyDay(dayjs__default.default(minDate));
  const _maxDate = common.copyDay(dayjs__default.default(maxDate));
  if (!_minDate.isValid() && !_maxDate.isValid()) {
    return void 0;
  }
  if (startDate && _minDate.isValid() && startTime.filter(Boolean).length) {
    const _startDate = common.copyDay(startDate).set("hour", parseInt(startTime[0] || "0")).set("minute", parseInt(startTime[1] || "")).set("second", parseInt(startTime[2] || "0"));
    if (_startDate.isBefore(_minDate)) {
      return t("components.set_start_time_failed", {
        time: _minDate.format("HH:mm:ss")
      });
    }
  }
  if (endDate && _maxDate.isValid() && endTime.filter(Boolean).length) {
    const _endDate = common.copyDay(endDate).set("hour", parseInt(endTime[0] || "0")).set("minute", parseInt(endTime[1] || "")).set("second", parseInt(endTime[2] || "0"));
    if (_endDate.isAfter(_maxDate)) {
      return t("components.set_end_time_failed", {
        time: _maxDate.format("HH:mm:ss")
      });
    }
  }
  return void 0;
}
const Time = React__default.default.forwardRef((props, ref) => {
  const { range, minDate, maxDate, onOk } = props;
  const { t } = useParrotTranslation();
  const initRange = useMemoCompare(range, (prev, next) => {
    const prevStringList = prev == null ? void 0 : prev.map((item) => item == null ? void 0 : item.format());
    const nextStringList = next == null ? void 0 : next.map((item) => item == null ? void 0 : item.format());
    return _.isEqual(prevStringList, nextStringList);
  });
  const [startTime, setStartTime] = React.useState(
    transformTime("start", initRange == null ? void 0 : initRange[0], minDate)
  );
  const [endTime, setEndTime] = React.useState(
    transformTime("end", initRange == null ? void 0 : initRange[1], maxDate)
  );
  const [error, setError] = React.useState("");
  const [start, end] = props.range;
  const startDate = start == null ? void 0 : start.format("YYYY-MM-DD");
  const endDate = end == null ? void 0 : end.format("YYYY-MM-DD");
  function reset() {
    setError("");
    setStartTime(transformTime("start", initRange == null ? void 0 : initRange[0], minDate));
    setEndTime(transformTime("end", initRange == null ? void 0 : initRange[1], maxDate));
  }
  React.useImperativeHandle(ref, () => ({ reset }));
  React.useEffect(() => {
    reset();
  }, [initRange]);
  const validateTimeRange = React.useMemo(() => {
    if (startDate && endDate) {
      let _startDate = common.copyDay(dayjs__default.default(startDate));
      let _endDate = common.copyDay(dayjs__default.default(endDate));
      _startDate = _startDate.set("hour", parseInt(startTime[0] || "0")).set("minute", parseInt(startTime[1] || "0")).set("second", parseInt(startTime[2] || "0"));
      _endDate = _endDate.set("hour", parseInt(endTime[0] || "0")).set("minute", parseInt(endTime[1] || "0")).set("second", parseInt(endTime[2] || "0"));
      return _startDate.valueOf() <= _endDate.valueOf();
    }
    return false;
  }, [endDate, endTime, startDate, startTime]);
  function handleOk() {
    if (!startDate || !endDate) {
      return;
    }
    const validateResult = checkMinAndMaxTimeRange(
      t,
      range,
      [startTime, endTime],
      minDate,
      maxDate
    );
    if (validateResult) {
      setError(validateResult);
      return;
    } else if (!validateTimeRange && start && end) {
      setError(t("components.end_time_cannot_be_less_than_start_time"));
      return;
    } else {
      setError("");
    }
    onOk([startTime, endTime]);
  }
  return /* @__PURE__ */ React__default.default.createElement(DateRangePicker_style.AbsoluteTimeStyle.Time, null, /* @__PURE__ */ React__default.default.createElement("div", { className: "time-input-main" }, /* @__PURE__ */ React__default.default.createElement("div", { className: "time-input-container" }, /* @__PURE__ */ React__default.default.createElement("div", { className: "start-time" }, /* @__PURE__ */ React__default.default.createElement("p", { className: core.cx("date", index.Typo.Label.l4_regular) }, startDate || t("components.start_date")), /* @__PURE__ */ React__default.default.createElement(
    InputTime,
    {
      value: startTime,
      danger: !!error,
      onChange: setStartTime
    }
  )), /* @__PURE__ */ React__default.default.createElement("div", { className: core.cx("to", index.Typo.Label.l3_regular) }, t("components.to")), /* @__PURE__ */ React__default.default.createElement("div", { className: "end-time" }, /* @__PURE__ */ React__default.default.createElement("p", { className: core.cx("date", index.Typo.Label.l4_regular) }, endDate || t("components.end_date")), /* @__PURE__ */ React__default.default.createElement(InputTime, { value: endTime, danger: !!error, onChange: setEndTime }))), /* @__PURE__ */ React__default.default.createElement(index$1, { disabled: !start || !end, type: "primary", onClick: handleOk }, t("components.confirm"))), error ? /* @__PURE__ */ React__default.default.createElement("p", { className: "time-input-error" }, error) : null);
});
const AbsoluteDate = React__default.default.forwardRef(
  (props, ref) => {
    const { range, minDate, maxDate, maxRange, onChange, onOk } = props;
    const timeRef = React.useRef(null);
    React.useImperativeHandle(ref, () => {
      var _a;
      return { reset: (_a = timeRef.current) == null ? void 0 : _a.reset };
    });
    function handleOk(timeRange) {
      const [startTime, endTime] = timeRange;
      const nowDate = dayjs__default.default();
      const startDate = updateDate(startTime, range[0] || nowDate);
      const endDate = updateDate(endTime, range[1] || nowDate);
      onOk == null ? void 0 : onOk([startDate, endDate]);
    }
    return /* @__PURE__ */ React__default.default.createElement(DateRangePicker_style.AbsoluteTimeStyle.Wrapper, null, /* @__PURE__ */ React__default.default.createElement(
      Calendar,
      {
        range,
        minDate,
        maxDate,
        maxRange,
        onChange
      }
    ), /* @__PURE__ */ React__default.default.createElement(
      Time,
      {
        ref: timeRef,
        range,
        minDate,
        maxDate,
        onOk: handleOk
      }
    ));
  }
);
var AbsoluteDate$1 = AbsoluteDate;

module.exports = AbsoluteDate$1;
