/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { blockClass } from "../../utils/util.js";
import React, { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { SelectItem, TimePicker, TimePickerSelect } from "@carbon/react";

//#region src/components/ConditionBuilder/ConditionBuilderItem/ConditionBuilderItemTime/ConditionBuilderItemTime.tsx
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const ConditionBuilderItemTime = ({ onChange, config, conditionState }) => {
	const getInitialValue = (itemType) => {
		return conditionState.value !== "INVALID" ? conditionState.value?.split(" ")[{
			time: 0,
			dayZone: 1,
			timeZone: 2
		}[itemType]] ?? void 0 : void 0;
	};
	const initialTimeValue = getInitialValue("time");
	const initialDayZone = getInitialValue("dayZone");
	const initialTimeZone = getInitialValue("timeZone");
	const [timeValue, setTimeValue] = useState(initialTimeValue);
	const [dayZoneValue, setDayZoneValue] = useState(initialDayZone ?? "AM");
	const [timeZoneValue, setTimeZoneValue] = useState(initialTimeZone ?? config?.timeZones?.[0]);
	useEffect(() => {
		const timeToUpdate = `${timeValue ?? ""} ${dayZoneValue ?? ""} ${timeZoneValue ?? ""}`;
		onChange(timeValue ? timeToUpdate : "INVALID");
	}, [
		timeValue,
		dayZoneValue,
		timeZoneValue
	]);
	const setTimeZone = (evt) => {
		setTimeZoneValue(evt.target.value);
	};
	const setDayZone = (evt) => {
		setDayZoneValue(evt.target.value);
	};
	const setTime = (evt) => {
		setTimeValue(evt.target.value);
	};
	return /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__item-time` }, /* @__PURE__ */ React.createElement(TimePicker, {
		defaultValue: initialTimeValue,
		id: "time-picker",
		labelText: "Select a time",
		onChange: setTime
	}, /* @__PURE__ */ React.createElement(TimePickerSelect, {
		id: "time-picker-day-zone",
		defaultValue: initialDayZone,
		onChange: setDayZone
	}, /* @__PURE__ */ React.createElement(SelectItem, {
		value: "AM",
		text: "AM"
	}), /* @__PURE__ */ React.createElement(SelectItem, {
		value: "PM",
		text: "PM"
	})), /* @__PURE__ */ React.createElement(TimePickerSelect, {
		id: "time-picker-time-zone",
		defaultValue: initialTimeZone,
		onChange: setTimeZone
	}, config?.timeZones?.map((timeZone, index) => /* @__PURE__ */ React.createElement(SelectItem, {
		key: index,
		value: timeZone,
		text: timeZone
	})))));
};
ConditionBuilderItemTime.propTypes = {
	conditionState: PropTypes.object,
	config: PropTypes.object,
	onChange: PropTypes.func
};

//#endregion
export { ConditionBuilderItemTime };