/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch */
import Timezone from "./timezone.js";
// needed for typescript type resolution
// eslint-disable-next-line no-unused-vars
import Component from "./component.js";
let zones = null;
/**
* @classdesc
* Singleton class to contain timezones. Right now it is all manual registry in
* the future we may use this class to download timezone information or handle
* loading pre-expanded timezones.
*
* @exports module:ICAL.TimezoneService
* @memberof ICAL
*/
const TimezoneService = {
get count() {
if (zones === null) {
return 0;
}
return Object.keys(zones).length;
},
reset: function() {
zones = Object.create(null);
let utc = Timezone.utcTimezone;
zones.Z = utc;
zones.UTC = utc;
zones.GMT = utc;
},
_hard_reset: function() {
zones = null;
},
/**
* Checks if timezone id has been registered.
*
* @param {String} tzid Timezone identifier (e.g. America/Los_Angeles)
* @return {Boolean} False, when not present
*/
has: function(tzid) {
if (zones === null) {
return false;
}
return !!zones[tzid];
},
/**
* Returns a timezone by its tzid if present.
*
* @param {String} tzid Timezone identifier (e.g. America/Los_Angeles)
* @return {Timezone | undefined} The timezone, or undefined if not found
*/
get: function(tzid) {
if (zones === null) {
this.reset();
}
return zones[tzid];
},
/**
* Registers a timezone object or component.
*
* @param {Component|Timezone} timezone
* The initialized zone or vtimezone.
*
* @param {String=} name
* The name of the timezone. Defaults to the component's TZID if not
* passed.
*/
register: function(timezone, name) {
if (zones === null) {
this.reset();
}
// This avoids a breaking change by the change of argument order
// TODO remove in v3
if (typeof timezone === "string" && name instanceof Timezone) {
[timezone, name] = [name, timezone];
}
if (!name) {
if (timezone instanceof Timezone) {
name = timezone.tzid;
} else {
if (timezone.name === 'vtimezone') {
timezone = new Timezone(timezone);
name = timezone.tzid;
}
}
}
if (!name) {
throw new TypeError("Neither a timezone nor a name was passed");
}
if (timezone instanceof Timezone) {
zones[name] = timezone;
} else {
throw new TypeError('timezone must be ICAL.Timezone or ICAL.Component');
}
},
/**
* Removes a timezone by its tzid from the list.
*
* @param {String} tzid Timezone identifier (e.g. America/Los_Angeles)
* @return {?Timezone} The removed timezone, or null if not registered
*/
remove: function(tzid) {
if (zones === null) {
return null;
}
return (delete zones[tzid]);
}
};
export default TimezoneService;