import O from "./index-extends.js";
import * as c from "react";
import { useOpenState as w } from "./index-useOpenState.js";
import { useUtils as P } from "./index-useUtils.js";
const K = (h, t) => {
  const {
    onAccept: C,
    onChange: r,
    value: n,
    closeOnSelect: f
  } = h, a = P(), {
    isOpen: u,
    setIsOpen: d
  } = w(h), o = c.useMemo(() => t.parseInput(a, n), [t, a, n]), [k, A] = c.useState(o), [s, S] = c.useState(() => ({
    committed: o,
    draft: o,
    resetFallback: o
  })), l = c.useCallback((e) => {
    S((i) => {
      switch (e.action) {
        case "setAll":
        case "acceptAndClose":
          return {
            draft: e.value,
            committed: e.value,
            resetFallback: e.value
          };
        case "setCommitted":
          return O({}, i, {
            draft: e.value,
            committed: e.value
          });
        case "setDraft":
          return O({}, i, {
            draft: e.value
          });
        default:
          return i;
      }
    }), (e.forceOnChangeCall || !e.skipOnChangeCall && !t.areValuesEqual(a, s.committed, e.value)) && r(e.value), e.action === "acceptAndClose" && (d(!1), C && !t.areValuesEqual(a, s.resetFallback, e.value) && C(e.value));
  }, [C, r, d, s, a, t]);
  c.useEffect(() => {
    a.isValid(o) && A(o);
  }, [a, o]), c.useEffect(() => {
    u && l({
      action: "setAll",
      value: o,
      skipOnChangeCall: !0
    });
  }, [u]), t.areValuesEqual(a, s.committed, o) || l({
    action: "setCommitted",
    value: o,
    skipOnChangeCall: !0
  });
  const v = c.useMemo(() => ({
    open: u,
    onClear: () => {
      l({
        value: t.emptyValue,
        action: "acceptAndClose",
        // force `onChange` in cases like input (value) === `Invalid date`
        forceOnChangeCall: !t.areValuesEqual(a, n, t.emptyValue)
      });
    },
    onAccept: () => {
      l({
        value: s.draft,
        action: "acceptAndClose",
        // force `onChange` in cases like input (value) === `Invalid date`
        forceOnChangeCall: !t.areValuesEqual(a, n, o)
      });
    },
    onDismiss: () => {
      l({
        value: s.committed,
        action: "acceptAndClose"
      });
    },
    onCancel: () => {
      l({
        value: s.resetFallback,
        action: "acceptAndClose"
      });
    },
    onSetToday: () => {
      l({
        value: t.getTodayValue(a),
        action: "acceptAndClose"
      });
    }
  }), [l, u, a, s, t, n, o]), [p, D] = c.useState(!1), E = c.useMemo(() => ({
    parsedValue: s.draft,
    isMobileKeyboardViewOpen: p,
    toggleMobileKeyboardView: () => D(!p),
    onDateChange: (e, i, V = "partial") => {
      switch (V) {
        case "shallow":
          return l({
            action: "setDraft",
            value: e,
            skipOnChangeCall: !0
          });
        case "partial":
          return l({
            action: "setDraft",
            value: e
          });
        case "finish":
          return l(f ?? i === "desktop" ? {
            value: e,
            action: "acceptAndClose"
          } : {
            value: e,
            action: "setCommitted"
          });
        default:
          throw new Error("MUI: Invalid selectionState passed to `onDateChange`");
      }
    }
  }), [l, p, s.draft, f]), b = c.useCallback((e, i) => {
    const V = t.valueReducer ? t.valueReducer(a, k, e) : e;
    r(V, i);
  }, [r, t, k, a]), y = c.useMemo(() => ({
    onChange: b,
    open: u,
    rawValue: n,
    openPicker: () => d(!0)
  }), [b, u, n, d]), m = {
    pickerProps: E,
    inputProps: y,
    wrapperProps: v
  };
  return c.useDebugValue(m, () => ({
    MuiPickerState: {
      dateState: s,
      other: m
    }
  })), m;
};
export {
  K as usePickerState
};
