/*
 * SAPUI5
 * (c) Copyright 2026 SAP SE. All rights reserved.
 */

/**
 * DateTimeWithTimezone data type that supports field-control and year 0000 validation.
 *
 * @name sap.ui.comp.smartfield.type.DateTimeWithTimezone
 * @author SAP SE
 * @version 1.148.0
 * @private
 * @extends sap.ui.model.odata.type.DateTimeWithTimezone
 * @returns {sap.ui.comp.smartfield.type.DateTimeWithTimezone} the DateTimeWithTimezone implementation.
 */
sap.ui.define([
	"sap/ui/core/Lib",
	"sap/ui/model/odata/type/DateTimeWithTimezone",
	"sap/ui/model/ValidateException"
], function(Library, DateTimeWithTimezoneBase, ValidateException) {
	"use strict";

	/**
	 * Constructor for a composite type <code>DateTimeWithTimezone</code>.
	 *
	 * @param {object} oFormatOptions format options.
	 * @param {object} oConstraints constraints.
	 * @private
	 */
	const DateTimeWithTimezone = DateTimeWithTimezoneBase.extend("sap.ui.comp.smartfield.type.DateTimeWithTimezone", {
		constructor: function(oFormatOptions, oConstraints) {
			DateTimeWithTimezoneBase.apply(this, arguments);
			this.oFieldControl = null;
		}
	});

	/**
	 * Parses the given value and calls the field control callback if set.
	 *
	 * @param {string} sValue the value to be parsed
	 * @param {string} sSourceType the source type
	 * @param {Array} aCurrentValues the current values
	 * @returns {Array} the parsed value as [Date, timezone]
	 * @throws {sap.ui.model.ParseException} if the value cannot be parsed
	 * @public
	 */
	DateTimeWithTimezone.prototype.parseValue = function(sValue, sSourceType, aCurrentValues) {
		const aReturn = DateTimeWithTimezoneBase.prototype.parseValue.apply(this, arguments);

		if (typeof this.oFieldControl === "function") {
			this.oFieldControl(sValue, sSourceType);
		}

		return aReturn;
	};

	/**
	 * Validates the given values. Rejects year 0000 dates.
	 *
	 * @param {Array} aValues the values to validate as [Date, timezone]
	 * @throws {sap.ui.model.ValidateException} if the date has year 0000
	 * @public
	 */
	DateTimeWithTimezone.prototype.validateValue = function(aValues) {
		DateTimeWithTimezoneBase.prototype.validateValue.apply(this, arguments);

		if (Array.isArray(aValues) && aValues[0] instanceof Date && aValues[0].getFullYear() === 0) {
			throw new ValidateException(
				Library.getResourceBundleFor("sap.ui.comp").getText("SMARTFIELD_INVALID_ENTRY")
			);
		}
	};

	DateTimeWithTimezone.prototype.destroy = function() {
		DateTimeWithTimezoneBase.prototype.destroy.apply(this, arguments);
		this.oFieldControl = null;
	};

	/**
	 * Returns the type's name.
	 *
	 * @returns {string} the type's name
	 * @public
	 */
	DateTimeWithTimezone.prototype.getName = function() {
		return "sap.ui.comp.smartfield.type.DateTimeWithTimezone";
	};

	return DateTimeWithTimezone;
});
