import { jsx as m } from "react/jsx-runtime";
import "./index-DateRangeInput.scss.js";
import { useAppDispatch as h } from "./index-use-app-dispatch.js";
import { validInput as C } from "./index-valid-input.js";
import { InputBase as P } from "./index-InputBase.js";
import c from "./index-moment.js";
import { useState as k, useRef as x, useEffect as z } from "react";
import { Button as B, ButtonTypes as y, ButtonSizes as A } from "./index-Button.js";
import { ComponentContainer as _ } from "./index-ComponentContainer.js";
import { IconSize as b } from "./index-Icon.js";
import { IconNames as N } from "./index-IconNames.js";
import { ModalService as R } from "./index-ModalService.js";
import { DateRangeModal as T } from "./index-DateRangeModal.js";
import $ from "./index-InputAdornment.js";
const Z = (n) => {
  const {
    formconfig: e,
    inputFormatter: v
  } = n, {
    formatFn: d,
    reverseFormatFn: D
  } = v || {};
  let t = e == null ? void 0 : e._inputRef;
  const [j, M] = k({}), S = h();
  t || (t = x(null)), z(() => {
    setTimeout(() => {
      var a;
      (a = t == null ? void 0 : t.current) != null && a.value && (t.current.value = e == null ? void 0 : e.stateValue, M({}));
    }, 0);
  }, [e == null ? void 0 : e.stateValue]);
  const V = (a) => {
    var r, o, i, s;
    if (t != null && t.current) {
      const l = (o = (r = a == null ? void 0 : a.data) == null ? void 0 : r.selection) == null ? void 0 : o.startDate, f = (s = (i = a == null ? void 0 : a.data) == null ? void 0 : i.selection) == null ? void 0 : s.endDate;
      if (l && f) {
        const I = c(l).format("YYYY-MM-DD"), Y = c(f).format("YYYY-MM-DD");
        let u = `${I}_${Y}`;
        d && typeof d == "function" && (u = d(u)), t.current.value = u, e != null && e.setStateValue && (S(e.setStateValue(u)), C(n.formconfig));
      }
    }
  }, F = () => {
    var r;
    const a = {
      startDate: null,
      endDate: null
    };
    if ((r = t == null ? void 0 : t.current) != null && r.value) {
      let o = t.current.value;
      D && (o = D(o));
      const i = o.split("_"), s = c(i[0]), l = c(i[1]);
      s.isValid() && l.isValid() && (a.startDate = s, a.endDate = l);
    }
    R.open(m(T, {
      ...n.datePicker,
      initialValue: a,
      close: V
    }), {
      width: "640px"
    });
  }, p = {
    ...n
  };
  return delete p.datePicker, delete p.inputFormatter, m(_, {
    className: "DateRangeInput-container",
    children: m(P, {
      inputRef: t,
      formconfig: e,
      textFieldProps: {
        InputProps: {
          endAdornment: m($, {
            position: "end",
            children: m(B, {
              disabled: n.disabled,
              size: A.small,
              iconSize: b.large,
              iconLeft: N.calendar,
              type: y.default,
              iconColor: "--n500",
              onClick: () => {
                F();
              }
            })
          })
        }
      },
      ...p
    })
  });
};
export {
  Z as DateRangeInput
};
