"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/date/src/index.ts
var index_exports = {};
__export(index_exports, {
  __experimentalGetSettings: () => __experimentalGetSettings,
  date: () => date,
  dateI18n: () => dateI18n,
  format: () => format,
  getDate: () => getDate,
  getSettings: () => getSettings,
  gmdate: () => gmdate,
  gmdateI18n: () => gmdateI18n,
  humanTimeDiff: () => humanTimeDiff,
  isInTheFuture: () => isInTheFuture,
  setSettings: () => setSettings
});
module.exports = __toCommonJS(index_exports);
var import_moment = __toESM(require("moment"));
var import_moment_timezone = require("moment-timezone/moment-timezone.js");
var import_moment_timezone_utils = require("moment-timezone/moment-timezone-utils.js");
var import_deprecated = __toESM(require("@wordpress/deprecated"));
var WP_ZONE = "WP";
var VALID_UTC_OFFSET = /^[+-][0-1][0-9](:?[0-9][0-9])?$/;
var settings = {
  l10n: {
    locale: "en",
    months: [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
    ],
    monthsShort: [
      "Jan",
      "Feb",
      "Mar",
      "Apr",
      "May",
      "Jun",
      "Jul",
      "Aug",
      "Sep",
      "Oct",
      "Nov",
      "Dec"
    ],
    weekdays: [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
    ],
    weekdaysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    meridiem: { am: "am", pm: "pm", AM: "AM", PM: "PM" },
    relative: {
      future: "%s from now",
      past: "%s ago",
      s: "a few seconds",
      ss: "%d seconds",
      m: "a minute",
      mm: "%d minutes",
      h: "an hour",
      hh: "%d hours",
      d: "a day",
      dd: "%d days",
      M: "a month",
      MM: "%d months",
      y: "a year",
      yy: "%d years"
    },
    startOfWeek: 0
  },
  formats: {
    time: "g:i a",
    date: "F j, Y",
    datetime: "F j, Y g:i a",
    datetimeAbbreviated: "M j, Y g:i a"
  },
  timezone: { offset: 0, offsetFormatted: "0", string: "", abbr: "" }
};
function setSettings(dateSettings) {
  settings = dateSettings;
  setupWPTimezone();
  if (import_moment.default.locales().includes(dateSettings.l10n.locale)) {
    if (import_moment.default.localeData(dateSettings.l10n.locale).longDateFormat("LTS") === null) {
      import_moment.default.defineLocale(dateSettings.l10n.locale, null);
    } else {
      return;
    }
  }
  const currentLocale = import_moment.default.locale();
  import_moment.default.defineLocale(dateSettings.l10n.locale, {
    // Inherit anything missing from English. We don't load
    // moment-with-locales.js so English is all there is.
    parentLocale: "en",
    months: dateSettings.l10n.months,
    monthsShort: dateSettings.l10n.monthsShort,
    weekdays: dateSettings.l10n.weekdays,
    weekdaysShort: dateSettings.l10n.weekdaysShort,
    meridiem(hour, minute, isLowercase) {
      if (hour < 12) {
        return isLowercase ? dateSettings.l10n.meridiem.am : dateSettings.l10n.meridiem.AM;
      }
      return isLowercase ? dateSettings.l10n.meridiem.pm : dateSettings.l10n.meridiem.PM;
    },
    longDateFormat: {
      LT: dateSettings.formats.time,
      LTS: import_moment.default.localeData("en").longDateFormat("LTS"),
      L: import_moment.default.localeData("en").longDateFormat("L"),
      LL: dateSettings.formats.date,
      LLL: dateSettings.formats.datetime,
      LLLL: import_moment.default.localeData("en").longDateFormat("LLLL")
    },
    // From human_time_diff?
    // Set to `(number, withoutSuffix, key, isFuture) => {}` instead.
    relativeTime: dateSettings.l10n.relative
  });
  import_moment.default.locale(currentLocale);
}
function getSettings() {
  return settings;
}
function __experimentalGetSettings() {
  (0, import_deprecated.default)("wp.date.__experimentalGetSettings", {
    since: "6.1",
    alternative: "wp.date.getSettings"
  });
  return getSettings();
}
function setupWPTimezone() {
  const currentTimezone = import_moment.default.tz.zone(settings.timezone.string);
  if (currentTimezone) {
    import_moment.default.tz.add(
      import_moment.default.tz.pack({
        name: WP_ZONE,
        abbrs: currentTimezone.abbrs,
        untils: currentTimezone.untils,
        offsets: currentTimezone.offsets
      })
    );
  } else {
    import_moment.default.tz.add(
      import_moment.default.tz.pack({
        name: WP_ZONE,
        abbrs: [WP_ZONE],
        untils: [null],
        offsets: [-settings.timezone.offset * 60 || 0]
      })
    );
  }
}
var MINUTE_IN_SECONDS = 60;
var HOUR_IN_MINUTES = 60;
var HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS;
var formatMap = {
  // Day.
  d: "DD",
  D: "ddd",
  j: "D",
  l: "dddd",
  N: "E",
  /**
   * Gets the ordinal suffix.
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  S(momentDate) {
    const num = momentDate.format("D");
    const withOrdinal = momentDate.format("Do");
    return withOrdinal.replace(num, "");
  },
  w: "d",
  /**
   * Gets the day of the year (zero-indexed).
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  z(momentDate) {
    return (parseInt(momentDate.format("DDD"), 10) - 1).toString();
  },
  // Week.
  W: "W",
  // Month.
  F: "MMMM",
  m: "MM",
  M: "MMM",
  n: "M",
  /**
   * Gets the days in the month.
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  t(momentDate) {
    return momentDate.daysInMonth();
  },
  // Year.
  /**
   * Gets whether the current year is a leap year.
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  L(momentDate) {
    return momentDate.isLeapYear() ? "1" : "0";
  },
  o: "GGGG",
  Y: "YYYY",
  y: "YY",
  // Time.
  a: "a",
  A: "A",
  /**
   * Gets the current time in Swatch Internet Time (.beats).
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  B(momentDate) {
    const timezoned = (0, import_moment.default)(momentDate).utcOffset(60);
    const seconds = parseInt(timezoned.format("s"), 10), minutes = parseInt(timezoned.format("m"), 10), hours = parseInt(timezoned.format("H"), 10);
    return parseInt(
      ((seconds + minutes * MINUTE_IN_SECONDS + hours * HOUR_IN_SECONDS) / 86.4).toString(),
      10
    );
  },
  g: "h",
  G: "H",
  h: "hh",
  H: "HH",
  i: "mm",
  s: "ss",
  u: "SSSSSS",
  v: "SSS",
  // Timezone.
  e: "zz",
  /**
   * Gets whether the timezone is in DST currently.
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  I(momentDate) {
    return momentDate.isDST() ? "1" : "0";
  },
  O: "ZZ",
  P: "Z",
  T: "z",
  /**
   * Gets the timezone offset in seconds.
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  Z(momentDate) {
    const offset = momentDate.format("Z");
    const sign = offset[0] === "-" ? -1 : 1;
    const parts = offset.substring(1).split(":").map((n) => parseInt(n, 10));
    return sign * (parts[0] * HOUR_IN_MINUTES + parts[1]) * MINUTE_IN_SECONDS;
  },
  // Full date/time.
  c: "YYYY-MM-DDTHH:mm:ssZ",
  // .toISOString.
  /**
   * Formats the date as RFC2822.
   *
   * @param momentDate Moment instance.
   *
   * @return Formatted date.
   */
  r(momentDate) {
    return momentDate.locale("en").format("ddd, DD MMM YYYY HH:mm:ss ZZ");
  },
  U: "X"
};
function format(dateFormat, dateValue = /* @__PURE__ */ new Date()) {
  let i, char;
  const newFormat = [];
  const momentDate = (0, import_moment.default)(dateValue);
  for (i = 0; i < dateFormat.length; i++) {
    char = dateFormat[i];
    if ("\\" === char) {
      i++;
      newFormat.push("[" + dateFormat[i] + "]");
      continue;
    }
    if (char in formatMap) {
      const formatter = formatMap[char];
      if (typeof formatter !== "string") {
        newFormat.push("[" + formatter(momentDate) + "]");
      } else {
        newFormat.push(formatter);
      }
    } else {
      newFormat.push("[" + char + "]");
    }
  }
  return momentDate.format(newFormat.join("[]"));
}
function date(dateFormat, dateValue = /* @__PURE__ */ new Date(), timezone) {
  const dateMoment = buildMoment(dateValue, timezone);
  return format(dateFormat, dateMoment);
}
function gmdate(dateFormat, dateValue = /* @__PURE__ */ new Date()) {
  const dateMoment = (0, import_moment.default)(dateValue).utc();
  return format(dateFormat, dateMoment);
}
function dateI18n(dateFormat, dateValue = /* @__PURE__ */ new Date(), timezone) {
  if (true === timezone) {
    return gmdateI18n(dateFormat, dateValue);
  }
  if (false === timezone) {
    timezone = void 0;
  }
  const dateMoment = buildMoment(dateValue, timezone);
  dateMoment.locale(settings.l10n.locale);
  return format(dateFormat, dateMoment);
}
function gmdateI18n(dateFormat, dateValue = /* @__PURE__ */ new Date()) {
  const dateMoment = (0, import_moment.default)(dateValue).utc();
  dateMoment.locale(settings.l10n.locale);
  return format(dateFormat, dateMoment);
}
function isInTheFuture(dateValue) {
  const now = import_moment.default.tz(WP_ZONE);
  const momentObject = import_moment.default.tz(dateValue, WP_ZONE);
  return momentObject.isAfter(now);
}
function getDate(dateString) {
  if (!dateString) {
    return import_moment.default.tz(WP_ZONE).toDate();
  }
  return import_moment.default.tz(dateString, WP_ZONE).toDate();
}
function humanTimeDiff(from, to) {
  const fromMoment = import_moment.default.tz(from, WP_ZONE);
  const toMoment = to ? import_moment.default.tz(to, WP_ZONE) : import_moment.default.tz(WP_ZONE);
  return fromMoment.from(toMoment);
}
function buildMoment(dateValue, timezone = "") {
  const dateMoment = (0, import_moment.default)(dateValue);
  if (timezone !== "") {
    return isUTCOffset(timezone) ? dateMoment.utcOffset(timezone) : (
      // A false isUTCOffset() guarantees that timezone is a string.
      dateMoment.tz(timezone)
    );
  }
  if (settings.timezone.string) {
    return dateMoment.tz(settings.timezone.string);
  }
  return dateMoment.utcOffset(+settings.timezone.offset);
}
function isUTCOffset(offset) {
  if ("number" === typeof offset) {
    return true;
  }
  return VALID_UTC_OFFSET.test(offset);
}
setupWPTimezone();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  __experimentalGetSettings,
  date,
  dateI18n,
  format,
  getDate,
  getSettings,
  gmdate,
  gmdateI18n,
  humanTimeDiff,
  isInTheFuture,
  setSettings
});
//# sourceMappingURL=index.cjs.map
