import {
  DatePickerButton
} from "./chunk-3HBRMWRE.js";
import {
  TimeSelect
} from "./chunk-HXUZQJQY.js";
import {
  getTimeOption,
  parseTimeOption
} from "./chunk-VAXB7NV2.js";
import {
  Form
} from "./chunk-PYR4SVP5.js";
import {
  Grid
} from "./chunk-EGEQY3KT.js";
import {
  Dialog
} from "./chunk-JHZGOPC4.js";
import {
  Separator
} from "./chunk-HYITAA4J.js";
import {
  toast
} from "./chunk-CYYWMYE7.js";
import {
  Box
} from "./chunk-4Y5BEXVN.js";
import {
  Flex
} from "./chunk-25HMMI7R.js";
import {
  Button
} from "./chunk-U7JPP7WJ.js";
import {
  __spreadProps,
  __spreadValues
} from "./chunk-N552FDTV.js";

// src/app/platform/edit-coaching-time.tsx
import { useCallback, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
var getValueFromDefaultValue = (args) => {
  return {
    startTime: args.startAt ? getTimeOption(args.startAt) : "00:00",
    endTime: args.endAt ? getTimeOption(args.endAt) : "00:00",
    date: args.startAt || /* @__PURE__ */ new Date()
  };
};
var getDateFromValue = (date, time) => {
  const { hour, minute } = parseTimeOption(time);
  return new Date(
    date.getFullYear(),
    date.getMonth(),
    date.getDate(),
    hour,
    minute
  );
};
function EditCoachingTime(props) {
  const { defaultEndAt, defaultStartAt, onClickCancel } = props;
  const [values, setValues] = useState(() => {
    return getValueFromDefaultValue({
      startAt: defaultStartAt,
      endAt: defaultEndAt
    });
  });
  const [timeError, setTimeError] = useState("");
  const checkTimeError = useCallback(
    (startTime, endTime) => {
      const error = startTime >= endTime ? "\uC2DC\uC791\uC2DC\uAC04\uC744 \uC885\uB8CC\uC2DC\uAC04\uBCF4\uB2E4 \uBE60\uB974\uAC8C \uC120\uD0DD\uD574\uC8FC\uC138\uC694." : "";
      if (error) {
        setTimeError(error);
        return false;
      }
      return true;
    },
    []
  );
  const onChangeStartTime = useCallback(
    (v) => {
      checkTimeError(v, values.endTime);
      setValues((prev) => __spreadProps(__spreadValues({}, prev), {
        startTime: v
      }));
    },
    [checkTimeError, values.endTime]
  );
  const onChangeEndTime = useCallback(
    (v) => {
      checkTimeError(values.startTime, v);
      setValues((prev) => __spreadProps(__spreadValues({}, prev), {
        endTime: v
      }));
    },
    [checkTimeError, values.startTime]
  );
  const onChangeDate = useCallback((date) => {
    date && setValues((prev) => __spreadProps(__spreadValues({}, prev), {
      date
    }));
  }, []);
  const onClickSave = useCallback(() => {
    if (checkTimeError(values.startTime, values.endTime)) {
      const startAt = getDateFromValue(values.date, values.startTime);
      const endAt = getDateFromValue(values.date, values.endTime);
      props.onClickSave && props.onClickSave(startAt, endAt);
    } else {
      toast.error("\uC785\uB825 \uAC12\uC774 \uC720\uD6A8\uD558\uC9C0 \uC54A\uC2B5\uB2C8\uB2E4.");
    }
  }, [checkTimeError, props, values.date, values.endTime, values.startTime]);
  return /* @__PURE__ */ jsx(Box, { width: { initial: "100%" }, children: /* @__PURE__ */ jsxs(Form.Root, { children: [
    /* @__PURE__ */ jsxs(Form.Field, { name: "date", children: [
      /* @__PURE__ */ jsx(Form.Label, { variant: "caption", children: "\uB0A0\uC9DC" }),
      /* @__PURE__ */ jsx(
        DatePickerButton,
        {
          maxDate: /* @__PURE__ */ new Date(),
          onChange: onChangeDate,
          selected: values.date,
          size: { initial: "large", xs: "medium" }
        }
      )
    ] }),
    /* @__PURE__ */ jsxs(
      Grid,
      {
        align: "center",
        columns: {
          initial: "1fr 0 1fr",
          xs: "1fr auto 1fr"
        },
        gapX: "3",
        gapY: "2",
        justify: "center",
        mb: { initial: "5", xs: "4" },
        mt: "5",
        width: "100%",
        children: [
          /* @__PURE__ */ jsx(Form.Field, { name: "startTime", children: /* @__PURE__ */ jsx(Form.Label, { variant: "caption", children: "\uC2DC\uC791 \uC2DC\uAC04" }) }),
          /* @__PURE__ */ jsx("div", {}),
          /* @__PURE__ */ jsx(Form.Field, { name: "endTime", children: /* @__PURE__ */ jsx(Form.Label, { variant: "caption", children: "\uC885\uB8CC \uC2DC\uAC04" }) }),
          /* @__PURE__ */ jsx(
            TimeSelect,
            {
              onValueChange: onChangeStartTime,
              size: { initial: "3", xs: "2" },
              value: values.startTime
            }
          ),
          /* @__PURE__ */ jsx(Box, { overflow: "hidden", children: /* @__PURE__ */ jsx(Separator, {}) }),
          /* @__PURE__ */ jsx(
            TimeSelect,
            {
              onValueChange: onChangeEndTime,
              size: { initial: "3", xs: "2" },
              value: values.endTime
            }
          ),
          /* @__PURE__ */ jsx(Form.Field, { name: "startEndTime", style: { gridColumn: "1/span 3" }, children: /* @__PURE__ */ jsx(Form.Message, { forceMatch: Boolean(timeError), children: timeError }) })
        ]
      }
    ),
    /* @__PURE__ */ jsxs(Flex, { gap: "3", justify: "end", children: [
      /* @__PURE__ */ jsx(Dialog.Close, { children: /* @__PURE__ */ jsx(Button, { onClick: onClickCancel, type: "button", variant: "outline", children: "\uCDE8\uC18C\uD558\uAE30" }) }),
      /* @__PURE__ */ jsx(Dialog.Close, { children: /* @__PURE__ */ jsx(Button, { onClick: onClickSave, type: "button", children: "\uC800\uC7A5\uD558\uAE30" }) })
    ] })
  ] }) });
}

export {
  EditCoachingTime
};
//# sourceMappingURL=chunk-AYQLLMMQ.js.map