"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/components/src/date-time/time/index.tsx
var time_exports = {};
__export(time_exports, {
  TimePicker: () => TimePicker,
  default: () => time_default
});
module.exports = __toCommonJS(time_exports);
var import_date_fns = require("date-fns");
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_date = require("@wordpress/date");
var import_base_control = __toESM(require("../../base-control/index.cjs"), 1);
var import_visually_hidden = require("../../visually-hidden/index.cjs");
var import_select_control = __toESM(require("../../select-control/index.cjs"), 1);
var import_timezone = __toESM(require("./timezone.cjs"), 1);
var import_styles = require("./styles.cjs");
var import_h_stack = require("../../h-stack/index.cjs");
var import_spacer = require("../../spacer/index.cjs");
var import_utils = require("../utils.cjs");
var import_constants = require("../constants.cjs");
var import_time_input = require("./time-input/index.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var VALID_DATE_ORDERS = ["dmy", "mdy", "ymd"];
function TimePicker({
  is12Hour,
  currentTime,
  onChange,
  dateOrder: dateOrderProp,
  hideLabelFromVision = false
}) {
  const [date, setDate] = (0, import_element.useState)(() => (
    // Truncate the date at the minutes, see: #15495.
    (0, import_date_fns.startOfMinute)((0, import_utils.inputToDate)(currentTime !== null && currentTime !== void 0 ? currentTime : /* @__PURE__ */ new Date()))
  ));
  (0, import_element.useEffect)(() => {
    setDate((0, import_date_fns.startOfMinute)((0, import_utils.inputToDate)(currentTime !== null && currentTime !== void 0 ? currentTime : /* @__PURE__ */ new Date())));
  }, [currentTime]);
  const monthOptions = [{
    value: "01",
    label: (0, import_i18n.__)("January")
  }, {
    value: "02",
    label: (0, import_i18n.__)("February")
  }, {
    value: "03",
    label: (0, import_i18n.__)("March")
  }, {
    value: "04",
    label: (0, import_i18n.__)("April")
  }, {
    value: "05",
    label: (0, import_i18n.__)("May")
  }, {
    value: "06",
    label: (0, import_i18n.__)("June")
  }, {
    value: "07",
    label: (0, import_i18n.__)("July")
  }, {
    value: "08",
    label: (0, import_i18n.__)("August")
  }, {
    value: "09",
    label: (0, import_i18n.__)("September")
  }, {
    value: "10",
    label: (0, import_i18n.__)("October")
  }, {
    value: "11",
    label: (0, import_i18n.__)("November")
  }, {
    value: "12",
    label: (0, import_i18n.__)("December")
  }];
  const {
    day,
    month,
    year,
    minutes,
    hours
  } = (0, import_element.useMemo)(() => ({
    day: (0, import_date.date)("d", date),
    month: (0, import_date.date)("m", date),
    year: (0, import_date.date)("Y", date),
    minutes: (0, import_date.date)("i", date),
    hours: (0, import_date.date)("H", date)
  }), [date]);
  const buildNumberControlChangeCallback = (method) => {
    const callback = (value, {
      event
    }) => {
      if (!(0, import_utils.validateInputElementTarget)(event)) {
        return;
      }
      const numberValue = Number(value);
      const newDate = (0, import_utils.setInConfiguredTimezone)(date, {
        [method]: numberValue
      });
      setDate(newDate);
      onChange?.((0, import_date.date)(import_constants.TIMEZONELESS_FORMAT, newDate));
    };
    return callback;
  };
  const onTimeInputChangeCallback = ({
    hours: newHours,
    minutes: newMinutes
  }) => {
    const newDate = (0, import_utils.setInConfiguredTimezone)(date, {
      hours: newHours,
      minutes: newMinutes
    });
    setDate(newDate);
    onChange?.((0, import_date.date)(import_constants.TIMEZONELESS_FORMAT, newDate));
  };
  const dayField = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.DayInput, {
    className: "components-datetime__time-field components-datetime__time-field-day",
    label: (0, import_i18n.__)("Day"),
    hideLabelFromVision: true,
    __next40pxDefaultSize: true,
    value: day,
    step: 1,
    min: 1,
    max: 31,
    required: true,
    spinControls: "none",
    isPressEnterToChange: true,
    isDragEnabled: false,
    isShiftStepEnabled: false,
    onChange: buildNumberControlChangeCallback("date")
  }, "day");
  const monthField = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.MonthSelectWrapper, {
    children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_select_control.default, {
      className: "components-datetime__time-field components-datetime__time-field-month",
      label: (0, import_i18n.__)("Month"),
      hideLabelFromVision: true,
      __next40pxDefaultSize: true,
      value: month,
      options: monthOptions,
      onChange: (value) => {
        const newDate = (0, import_utils.setInConfiguredTimezone)(date, {
          month: Number(value) - 1
        });
        setDate(newDate);
        onChange?.((0, import_date.date)(import_constants.TIMEZONELESS_FORMAT, newDate));
      }
    })
  }, "month");
  const yearField = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.YearInput, {
    className: "components-datetime__time-field components-datetime__time-field-year",
    label: (0, import_i18n.__)("Year"),
    hideLabelFromVision: true,
    __next40pxDefaultSize: true,
    value: year,
    step: 1,
    min: 1,
    max: 9999,
    required: true,
    spinControls: "none",
    isPressEnterToChange: true,
    isDragEnabled: false,
    isShiftStepEnabled: false,
    onChange: buildNumberControlChangeCallback("year"),
    __unstableStateReducer: (0, import_utils.buildPadInputStateReducer)(4)
  }, "year");
  const defaultDateOrder = is12Hour ? "mdy" : "dmy";
  const dateOrder = dateOrderProp && VALID_DATE_ORDERS.includes(dateOrderProp) ? dateOrderProp : defaultDateOrder;
  const fields = dateOrder.split("").map((field) => {
    switch (field) {
      case "d":
        return dayField;
      case "m":
        return monthField;
      case "y":
        return yearField;
      default:
        return null;
    }
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Wrapper, {
    className: "components-datetime__time",
    children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Fieldset, {
      children: [hideLabelFromVision ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_visually_hidden.VisuallyHidden, {
        as: "legend",
        children: (0, import_i18n.__)("Time")
      }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_base_control.default.VisualLabel, {
        as: "legend",
        className: "components-datetime__time-legend",
        children: (0, import_i18n.__)("Time")
      }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_h_stack.HStack, {
        className: "components-datetime__time-wrapper",
        children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_time_input.TimeInput, {
          value: {
            hours: Number(hours),
            minutes: Number(minutes)
          },
          is12Hour,
          onChange: onTimeInputChangeCallback
        }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_spacer.Spacer, {}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_timezone.default, {})]
      })]
    }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Fieldset, {
      children: [hideLabelFromVision ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_visually_hidden.VisuallyHidden, {
        as: "legend",
        children: (0, import_i18n.__)("Date")
      }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_base_control.default.VisualLabel, {
        as: "legend",
        className: "components-datetime__time-legend",
        children: (0, import_i18n.__)("Date")
      }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_h_stack.HStack, {
        className: "components-datetime__time-wrapper",
        children: fields
      })]
    })]
  });
}
TimePicker.TimeInput = import_time_input.TimeInput;
Object.assign(TimePicker.TimeInput, {
  displayName: "TimePicker.TimeInput"
});
var time_default = TimePicker;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  TimePicker
});
//# sourceMappingURL=index.cjs.map
