import { t as React, u as useAtomValue, as as dayjs, j as jsxRuntimeExports, G as Grid, ai as Editor, at as LocalizationProvider, au as AdapterDayjs, av as DateCalendar, B as Button, z as currentTheme, ab as formEdit, ah as AxiosRequest } from "./index-D15x63tD.js";
function GetDateRange(value) {
  const params = /* @__PURE__ */ new Map();
  params.set("prefix", "ABLUtils").set("comand", "GetDateRange").set("Value", value);
  return AxiosRequest(true, params);
}
function SetDateRange(StartDate, EndDate) {
  const params = /* @__PURE__ */ new Map();
  params.set("prefix", "ABLUtils").set("comand", "SetDateRange").set("StartDate", StartDate).set("EndDate", EndDate);
  return AxiosRequest(true, params);
}
function DialogStartAndEndDate({ props }) {
  const [start, setStart] = React.useState();
  const [end, setEnd] = React.useState();
  const [calendarValue, setCalendarValue] = React.useState();
  const calendar = React.useRef();
  const switcher = React.useRef();
  const startStamp = React.useRef();
  const endStamp = React.useRef();
  const startRef = React.useRef();
  const endRef = React.useRef();
  const atomTheme = useAtomValue(currentTheme);
  const theme = `-${atomTheme}`;
  React.useEffect(() => {
    GetDateRange(props.ObjRef ? props.ObjRef : "0").then((res) => {
      setStart(res.StartDate ? res.StartDate : "");
      setEnd(res.EndDate ? res.EndDate : "");
      if (res.StartDate) {
        startStamp.current = getStamp(res.StartDate);
        setCalendarValue(dayjs(getDateInFormat(res.StartDate)));
      } else {
        setCalendarValue(dayjs(/* @__PURE__ */ new Date()));
      }
      if (res.EndDate) {
        endStamp.current = getStamp(res.EndDate);
      }
      setTimeout(() => {
        updatePeriod();
      });
      switcher.current = true;
      selectStart();
    });
  }, []);
  function getStamp(date) {
    return new Date(getDateInFormat(date)).getTime() + // корректировка по Часовому поясу
    (/* @__PURE__ */ new Date()).getTimezoneOffset() * 60 * 1e3;
  }
  function getDateInFormat(value) {
    let date = value;
    if (date) {
      if (date.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/)) return date;
      if (date.match(/[0-9]{2}.[0-9]{2}.[0-9]{4}/)) {
        date = date.split(".");
        date = [date[2], date[1], date[0]];
        return date.join("-");
      }
    }
  }
  function checkPeriod(timeStamp) {
    if (!endStamp.current || !startStamp.current) return;
    return startStamp.current < timeStamp && timeStamp < endStamp.current;
  }
  function updatePeriod() {
    const rowgroup = calendar.current.querySelector("[role=rowgroup]").children;
    for (let i = 0; i < rowgroup.length; i++) {
      const group = rowgroup[i].children;
      for (let j = 0; j < group.length; j++) {
        if (Number(group[j].dataset.timestamp) === startStamp.current) {
          group[j].classList.remove(`date-range${`${theme}`}`);
          group[j].classList.add(`date-range-start${`${theme}`}`);
          continue;
        } else {
          group[j].classList.remove(`date-range-start${`${theme}`}`);
        }
        if (Number(group[j].dataset.timestamp) === endStamp.current) {
          group[j].classList.remove(`date-range${`${theme}`}`);
          group[j].classList.add(`date-range-end${`${theme}`}`);
          continue;
        } else {
          group[j].classList.remove(`date-range-end${`${theme}`}`);
        }
        if (checkPeriod(Number(group[j].dataset.timestamp))) {
          group[j].classList.add(`date-range${`${theme}`}`);
        } else {
          group[j].classList.remove(`date-range${`${theme}`}`);
        }
      }
    }
  }
  function getDateStr(newValue) {
    return `${newValue.get("date") < 10 ? `0${newValue.get("date")}` : newValue.get("date")}.${newValue.month() + 1 < 10 ? `0${newValue.month() + 1}` : newValue.month() + 1}.${newValue.year()}`;
  }
  function checkStartDate(newValue) {
    if (!end) return;
    const endDate = new Date(getDateInFormat(end));
    const currentDate = new Date(getDateInFormat(getDateStr(newValue)));
    return endDate.getTime() < currentDate.getTime();
  }
  function checkEndDate(newValue) {
    if (!start) return;
    const startDate = new Date(getDateInFormat(start));
    const currentDate = new Date(getDateInFormat(getDateStr(newValue)));
    return startDate.getTime() > currentDate.getTime();
  }
  function selectEnd() {
    startRef.current.classList.remove("date-range-current");
    endRef.current.classList.add("date-range-current");
  }
  function selectStart() {
    startRef.current.classList.add("date-range-current");
    endRef.current.classList.remove("date-range-current");
  }
  function endClick() {
    selectEnd();
    switcher.current = false;
  }
  function startClick() {
    selectStart();
    switcher.current = true;
  }
  function onEditStart(data) {
    setCalendarValue(dayjs(getDateInFormat(data.value)));
    setStart(data.value);
    startStamp.current = getStamp(data.value);
    setTimeout(() => {
      if (startStamp.current && endStamp.current) {
        updatePeriod();
      }
    });
  }
  function onEditEnd(data) {
    setCalendarValue(dayjs(getDateInFormat(data.value)));
    setEnd(data.value);
    endStamp.current = getStamp(data.value);
    setTimeout(() => {
      if (startStamp.current && endStamp.current) {
        updatePeriod();
      }
    });
  }
  function onChangePeriod(newValue) {
    setCalendarValue(newValue);
    const value = getDateStr(newValue);
    if (switcher.current) {
      if (checkStartDate(newValue)) {
        setEnd(value);
        endStamp.current = getStamp(value);
        return;
      }
      setStart(value);
      startStamp.current = getStamp(value);
      selectEnd();
    } else {
      if (checkEndDate(newValue)) {
        setStart(value);
        startStamp.current = getStamp(value);
        return;
      }
      setEnd(value);
      endStamp.current = getStamp(value);
      selectStart();
    }
    switcher.current = !switcher.current;
  }
  async function onSelect() {
    const json = await SetDateRange(start, end);
    if (props.onEdit) {
      props.onEdit({
        value: json.Text,
        tag: json.Value,
        name: props.Name,
        record: props.record,
        textchanged: "0",
        setValue: props.setValue,
        setInputValue: props.setInputValue,
        type: props.Module,
        requestId: props.RequestID,
        addInfo: props.addInfo
      });
    }
    if (props.RequestID) {
      await formEdit(
        "1",
        props.func,
        {
          Name: "ObjRef",
          Value: json.Value
        },
        props.RequestID,
        props.from
      );
    }
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs(
      Grid,
      {
        container: true,
        direction: "column",
        justifyContent: "center",
        alignItems: "flex-start",
        style: { height: "calc(100% - 45px)" },
        children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs(
            Grid,
            {
              item: true,
              container: true,
              direction: "row",
              justifyContent: "center",
              alignItems: "flex-start",
              style: { height: "45px)" },
              children: [
                /* @__PURE__ */ jsxRuntimeExports.jsx(
                  Grid,
                  {
                    item: true,
                    style: {
                      height: "27px"
                    },
                    children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
                      Grid,
                      {
                        container: true,
                        direction: "row",
                        justifyContent: "flex-start",
                        alignItems: "flex-start",
                        style: { width: "158px", paddingLeft: "13px" },
                        children: [
                          /* @__PURE__ */ jsxRuntimeExports.jsx(
                            Grid,
                            {
                              style: {
                                position: "relative",
                                top: "-11px",
                                left: "20px",
                                backgroundColor: "white",
                                color: "#aaaaaa",
                                display: "flex",
                                width: "12px",
                                height: "12px",
                                justifyContent: "center"
                              },
                              children: "С"
                            }
                          ),
                          /* @__PURE__ */ jsxRuntimeExports.jsx(
                            Grid,
                            {
                              ref: startRef,
                              onClick: startClick,
                              style: {
                                border: "1px solid #cccccc",
                                height: "27px",
                                width: "100px"
                              },
                              children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { value: start, editStyle: 2, onEdit: onEditStart })
                            }
                          )
                        ]
                      }
                    )
                  }
                ),
                "-",
                /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, style: { height: "27px" }, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(
                  Grid,
                  {
                    container: true,
                    direction: "row",
                    justifyContent: "flex-start",
                    alignItems: "flex-start",
                    style: { width: "158px", paddingLeft: "15px" },
                    children: [
                      /* @__PURE__ */ jsxRuntimeExports.jsx(
                        Grid,
                        {
                          style: {
                            position: "relative",
                            top: "-11px",
                            left: "29px",
                            backgroundColor: "white",
                            color: "#aaaaaa",
                            display: "flex",
                            width: "20px",
                            height: "12px",
                            justifyContent: "center"
                          },
                          children: "По"
                        }
                      ),
                      /* @__PURE__ */ jsxRuntimeExports.jsx(
                        Grid,
                        {
                          ref: endRef,
                          onClick: endClick,
                          style: {
                            border: "1px solid #cccccc",
                            height: "27px",
                            width: "100px"
                          },
                          children: /* @__PURE__ */ jsxRuntimeExports.jsx(Editor, { value: end, editStyle: 2, onEdit: onEditEnd })
                        }
                      )
                    ]
                  }
                ) })
              ]
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, container: true, direction: "row", justifyContent: "center", alignItems: "flex-start", children: calendarValue !== void 0 ? /* @__PURE__ */ jsxRuntimeExports.jsx(Grid, { item: true, children: /* @__PURE__ */ jsxRuntimeExports.jsx(LocalizationProvider, { dateAdapter: AdapterDayjs, adapterLocale: "ru", children: /* @__PURE__ */ jsxRuntimeExports.jsx(
            DateCalendar,
            {
              showDaysOutsideCurrentMonth: true,
              views: ["day"],
              ref: calendar,
              value: calendarValue,
              onClick: () => {
                setTimeout(() => {
                  updatePeriod();
                });
              },
              onChange: onChangePeriod
            }
          ) }) }) : null })
        ]
      }
    ),
    /* @__PURE__ */ jsxRuntimeExports.jsx(
      Grid,
      {
        item: true,
        style: {
          width: "100%",
          height: "40px",
          marginTop: "5px"
        },
        children: /* @__PURE__ */ jsxRuntimeExports.jsxs(Grid, { container: true, direction: "row", justifyContent: "flex-end", alignItems: "center", style: { paddingTop: "8px" }, children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Button,
            {
              size: "small",
              variant: "outlined",
              style: { textTransform: "none", marginRight: "10px" },
              value: "1",
              className: "button_Modal_Select",
              onClick: onSelect,
              children: "Выбрать"
            }
          ),
          /* @__PURE__ */ jsxRuntimeExports.jsx(
            Button,
            {
              size: "small",
              variant: "outlined",
              style: { textTransform: "none" },
              value: "2",
              className: "button_Modal_Close",
              children: "Отмена"
            }
          )
        ] })
      }
    )
  ] });
}
export {
  DialogStartAndEndDate as default
};
//# sourceMappingURL=DialogStartAndEndDate-IteHdqr5.js.map
