(() => {
  class CountEverest {
    static DEFAULT_SETTINGS;
    constructor(e, t) {
      (CountEverest.DEFAULT_SETTINGS = {
        day: 1,
        month: 1,
        year: 2050,
        hour: 0,
        minute: 0,
        second: 0,
        countUp: !1,
        singularLabels: !0,
        leftHandZeros: !0,
        yearsLabel: 'Years',
        yearLabel: 'Year',
        monthsLabel: 'Months',
        monthLabel: 'Month',
        daysLabel: 'Days',
        dayLabel: 'Day',
        hoursLabel: 'Hours',
        hourLabel: 'Hour',
        minutesLabel: 'Minutes',
        minuteLabel: 'Minute',
        secondsLabel: 'Seconds',
        secondLabel: 'Second',
        accentColor: '#284ED8',
        units: ['years', 'months', 'days', 'hours', 'minutes', 'seconds'],
        onInit: null,
        afterCalculation: null,
        onChange: null,
      }),
        (this.#e = e),
        (this.#t = { ...CountEverest.DEFAULT_SETTINGS, ...t }),
        t._themeApplied || CountEverest.applyThemeOptions(e, this.#t),
        (e._ceSettings = this.#t),
        (this.#s = null),
        this.init();
    }
    #e;
    #t;
    #s;
    #n;
    init() {
      if (
        (this.createDOM(),
        this.#e.classList.contains('ce-countdown--theme-9') &&
          CountEverest.createCanvasesIfNeeded(this.#e),
        this.#e.classList.contains('ce-countdown--theme-6'))
      ) {
        const e = this.#t.accentColor;
        this.#e
          .querySelectorAll('.ce-flip-wrap .ce-flip-front, .ce-flip-wrap .ce-flip-back')
          .forEach((t) => {
            t.style.backgroundColor = e;
          });
      }
      this.setTargetDate(
        new Date(
          this.#t.year,
          this.#t.month - 1,
          this.#t.day,
          this.#t.hour,
          this.#t.minute,
          this.#t.second
        )
      ),
        this.calculate(),
        (this.#s = setInterval(() => this.calculate(), 1e3)),
        this.#t.onInit?.call(this);
    }
    calculate() {
      const e = new Date(),
        t = this.#n;
      let s = t - e;
      const n = Math.abs(s);
      s = this.#t.countUp ? e - t : Math.max(0, s);
      const i = 864e5,
        a = {};
      let c = s;
      [
        { name: 'years', ms: 31536e6 },
        { name: 'months', ms: 26298e5 },
        { name: 'days', ms: i },
        { name: 'hours', ms: 36e5 },
        { name: 'minutes', ms: 6e4 },
        { name: 'seconds', ms: 1e3 },
      ].forEach((e) => {
        this.#t.units.includes(e.name) && ((a[e.name] = Math.floor(c / e.ms)), (c %= e.ms));
      }),
        !this.#t.units.includes('days') ||
          this.#t.units.includes('years') ||
          this.#t.units.includes('months') ||
          (a.days = Math.floor(Math.abs(s) / i)),
        this.#e.classList.contains('ce-countdown--theme-10') &&
          this.#t.units.includes('days') &&
          (a.days = Math.floor(n / i)),
        Object.assign(this, a),
        'function' == typeof this.#t.afterCalculation && this.#t.afterCalculation.call(this),
        this.output(),
        s <= 0 && !this.#t.countUp && clearInterval(this.#s),
        'function' == typeof this.#t.onChange && this.#t.onChange.call(this, a);
    }
    output() {
      const e =
        this.#e.classList.contains('ce-countdown--theme-9') ||
        this.#e.classList.contains('ce-countdown--theme-10');
      let t;
      e
        ? (t = this.#t.units)
        : ((t = this.#t.units.filter((e) => (this[e] || 0) > 0)),
          0 === t.length &&
            this.#t.units.length > 0 &&
            t.push(this.#t.units[this.#t.units.length - 1])),
        t.forEach((e) => {
          const t = this[e] || 0;
          let s;
          s = this.#e.classList.contains('ce-countdown--theme-6')
            ? this.#e.querySelector(`.ce-${e} .ce-flip-back`)
            : this.#e.classList.contains('ce-countdown--theme-10')
              ? this.#e.querySelector(`.${e}`)
              : this.#e.querySelector(`.ce-${e}`);
          const n = this.#e.querySelector(`.ce-${e}-label`);
          s &&
            (this.#e.classList.contains('ce-countdown--theme-10') ||
              this.#e.classList.contains('ce-countdown--theme-9') ||
              (s.innerHTML = this.wrapDigits(t))),
            n && (n.textContent = this.getLabel(e, t));
        }),
        e ||
          this.#t.units.forEach((e) => {
            if (t.includes(e)) {
              const t = this.#e.querySelector(`.ce-${e}`),
                s = this.#e.querySelector(`.ce-col:has(.ce-${e})`);
              t && t.parentElement && (t.parentElement.style.display = ''),
                s && (s.style.display = '');
            } else {
              const t = this.#e.querySelector(`.ce-${e}`),
                s =
                  (this.#e.querySelector(`.ce-${e}-label`),
                  this.#e.querySelector(`.ce-col:has(.ce-${e})`));
              t && t.parentElement && (t.parentElement.style.display = 'none'),
                s && (s.style.display = 'none');
            }
          });
    }
    wrapDigits(e) {
      const t = e.toString();
      return (this.#t.leftHandZeros ? t.padStart(2, '0') : t)
        .split('')
        .map((e) => `<span class="ce-digit">${e}</span>`)
        .join('');
    }
    writeToDom(e, t) {
      const s = this.#e.querySelector(e);
      s && (s.textContent = t);
    }
    writeLabelToDom(e, t) {
      const s = this.#e.querySelector(e);
      s && (s.textContent = t);
    }
    getLabel(e, t) {
      const s = `${e.slice(0, -1)}Label`,
        n = `${e}Label`;
      return 1 === t && this.#t.singularLabels ? this.#t[s] : this.#t[n];
    }
    setTargetDate(e) {
      this.#n = e;
    }
    getTargetDate() {
      return this.#n;
    }
    destroy() {
      clearInterval(this.#s);
    }
    strPad(e, t, s = '0') {
      return String(e).padStart(t, s);
    }
    static autoInit(e = {}) {
      const t = { selector: '[data-ce-datetime]', rootMargin: '0px', threshold: 0.1, ...e };
      if (!('IntersectionObserver' in window)) return void CountEverest.initAllVisible(t.selector);
      const s = new IntersectionObserver(
        (e) => {
          e.forEach((e) => {
            e.isIntersecting &&
              !e.target.dataset.ceInitialized &&
              (CountEverest.initElement(e.target), s.unobserve(e.target));
          });
        },
        { rootMargin: t.rootMargin, threshold: t.threshold }
      );
      document.querySelectorAll(t.selector).forEach((e) => {
        e.dataset.ceInitialized || s.observe(e);
      });
    }
    static initAllVisible(e) {
      document.querySelectorAll(e).forEach((e) => {
        e.dataset.ceInitialized || CountEverest.initElement(e);
      });
    }
    static initElement(e) {
      const t = CountEverest.parseDataAttributes(e);
      CountEverest.applyThemeOptions(e, t),
        (t._themeApplied = !0),
        (e.dataset.ceInitialized = 'true'),
        new CountEverest(e, t);
    }
    static applyThemeOptions(e, t) {
      if (e.classList.contains('ce-countdown--theme-6'))
        (t.units && 6 !== t.units.length) || (t.units = ['days', 'hours', 'minutes', 'seconds']),
          (t.daysWrapper = '.ce-days .ce-flip-back'),
          (t.hoursWrapper = '.ce-hours .ce-flip-back'),
          (t.minutesWrapper = '.ce-minutes .ce-flip-back'),
          (t.secondsWrapper = '.ce-seconds .ce-flip-back'),
          (t.wrapDigits = !1),
          (t.onChange = function () {
            CountEverest.theme6Animate(e.querySelectorAll('.ce-col>div'));
          });
      else if (e.classList.contains('ce-countdown--theme-9'))
        (t.units && 6 !== t.units.length) || (t.units = ['days', 'hours', 'minutes', 'seconds']),
          (t.leftHandZeros = !1),
          (t.onChange = function () {
            CountEverest.theme9DrawCircles(e, this, t.accentColor || '#284ED8');
          });
      else if (e.classList.contains('ce-countdown--theme-10')) {
        let s = !0;
        (t.leftHandZeros = !0),
          (t.afterCalculation = function () {
            CountEverest.theme10FlipClock(e, this, s), (s = !1);
          });
      } else if (e.classList.contains('ce-countdown--theme-12') && t.accentColor) {
        e.style.color = t.accentColor;
        const s = t.onChange;
        t.onChange = function (...n) {
          e.querySelectorAll('.ce-digit').forEach((e) => {
            e.style.borderColor = t.accentColor;
          }),
            s && s.apply(this, n);
        };
      }
    }
    static theme6Animate(e) {
      e.forEach(function (e) {
        const t = e.querySelector('.ce-flip-front'),
          s = e.querySelector('.ce-flip-back').textContent,
          n = e.getAttribute('data-old');
        void 0 === n && e.setAttribute('data-old', s),
          s != n &&
            (e.classList.add('ce-animate'),
            window.setTimeout(function () {
              (t.textContent = s), e.classList.remove('ce-animate'), e.setAttribute('data-old', s);
            }, 800));
      });
    }
    static theme9DrawCircles(e, t, s = '#284ED8') {
      function deg(e) {
        return (Math.PI / 180) * e - Math.PI / 2;
      }
      const n = e._ceSettings || { units: ['days', 'hours', 'minutes', 'seconds'] },
        i = {
          years: { max: 100, value: t.years || 0, pad: !1 },
          months: { max: 12, value: t.months || 0, pad: !1 },
          days: { max: 365, value: t.days || 0, pad: !1 },
          hours: { max: 24, value: t.hours || 0, pad: !0 },
          minutes: { max: 60, value: t.minutes || 0, pad: !0 },
          seconds: { max: 60, value: t.seconds || 0, pad: !0 },
        };
      n.units.forEach((s) => {
        const n = i[s];
        if (n) {
          const i = e.querySelector(`.ce-${s}`);
          i && (i.textContent = n.pad ? t.strPad(n.value, 2) : n.value);
        }
      }),
        e.querySelectorAll('.ce-circle').forEach((e, t) => {
          const a = e.querySelector('canvas'),
            c = n.units.find((t) =>
              Array.from(e.querySelectorAll('[class*="ce-"]')).some((e) =>
                e.classList.contains(`ce-${t}`)
              )
            );
          if (a && c && i[c]) {
            const e = i[c];
            !(function drawCircle(e, t, s, n) {
              if (!e) return;
              const i = e.getContext('2d'),
                a = e.width / 2,
                c = e.height / 2,
                o = Math.min(a, c) - 0.05 * e.width,
                l = Math.max(3, 0.02 * e.width);
              i.clearRect(0, 0, e.width, e.height),
                (i.lineWidth = l),
                i.beginPath(),
                i.arc(a, c, o, 0, 2 * Math.PI),
                (i.strokeStyle = '#282828'),
                i.stroke(),
                i.beginPath(),
                (i.strokeStyle = n),
                i.arc(a, c, o, deg(0), deg((360 / s) * t)),
                i.stroke();
            })(a, e.value, e.max, s);
          }
        });
    }
    static theme10FlipClock(e, t, s) {
      const n = e._ceSettings || { units: ['days', 'hours', 'minutes', 'seconds'] },
        i = {};
      n.units.forEach((e) => {
        i[e] = t[e] || 0;
      }),
        s &&
          Object.entries(i).forEach(([t, s]) => {
            const i = e.querySelector(`.${t}`);
            if (!i) return;
            let a;
            a = n.leftHandZeros
              ? Math.max(2, s.toString().length)
              : Math.max(1, s.toString().length);
            const c = Array.from(
              { length: a },
              () =>
                `\n            <div class="ce-digits">\n              ${Array.from({ length: 10 }, (e, t) => `\n                <div class="ce-digits-inner">\n                  <div class="ce-flip-wrap">\n                    <div class="ce-up">\n                      <div class="ce-shadow"></div>\n                      <div class="ce-inn">${t}</div>\n                    </div>\n                    <div class="ce-down">\n                      <div class="ce-shadow"></div>\n                      <div class="ce-inn">${t}</div>\n                    </div>\n                  </div>\n                </div>\n              `).join('')}\n            </div>`
            ).join('');
            i.innerHTML = c;
          }),
        Object.entries(i).forEach(([s, i]) => {
          const a = e.querySelector(`.${s}`);
          if (!a) return;
          const c = a.querySelectorAll('.ce-digits').length;
          let o;
          o = n.leftHandZeros ? t.strPad(i.toString(), c, '0') : i.toString().padStart(c, '0');
          for (let e = 0; e < o.length; e++) {
            const t = a.querySelector(`.ce-digits:nth-child(${e + 1})`);
            if (!t) continue;
            const s = parseInt(o[e]);
            t.querySelectorAll('.ce-digits-inner').forEach((e, t) => {
              e.classList.remove('active', 'before'),
                t === s
                  ? e.classList.add('active')
                  : t === (s + 1) % 10 && e.classList.add('before');
            }),
              t.querySelector('.before') ? t.classList.add('play') : t.classList.remove('play');
          }
        });
    }
    static createCanvasesIfNeeded(e) {
      e.querySelectorAll('.ce-circle').forEach((e) => {
        if (!e.querySelector('canvas')) {
          const t = document.createElement('canvas');
          (t.width = 408), (t.height = 408), e.insertBefore(t, e.firstChild);
        }
      });
    }
    static parseDataAttributes(e) {
      const t = {},
        s = e.dataset.ceDatetime;
      if (s) {
        const e = CountEverest.parseDatetimeString(s);
        Object.assign(t, e);
      }
      ['countUp', 'singularLabels', 'leftHandZeros'].forEach((s) => {
        const n = e.dataset[`ce${s.charAt(0).toUpperCase() + s.slice(1)}`];
        void 0 !== n && (t[s] = 'true' === n || '' === n);
      }),
        [
          'yearsLabel',
          'yearLabel',
          'monthsLabel',
          'monthLabel',
          'daysLabel',
          'dayLabel',
          'hoursLabel',
          'hourLabel',
          'minutesLabel',
          'minuteLabel',
          'secondsLabel',
          'secondLabel',
          'accentColor',
        ].forEach((s) => {
          const n = e.dataset[`ce${s.charAt(0).toUpperCase() + s.slice(1)}`];
          void 0 !== n && (t[s] = n);
        });
      const n = e.dataset.ceUnits;
      return n && (t.units = n.split(',').map((e) => e.trim())), t;
    }
    static parseDatetimeString(e) {
      const t = {},
        [s, n] = e.trim().split(' ');
      if (s) {
        const e = s.split('-');
        e[0] && (t.year = parseInt(e[0], 10)),
          e[1] && (t.month = parseInt(e[1], 10)),
          e[2] && (t.day = parseInt(e[2], 10));
      }
      if (n) {
        const e = n.split(':');
        e[0] && (t.hour = parseInt(e[0], 10)),
          e[1] && (t.minute = parseInt(e[1], 10)),
          e[2] && (t.second = parseInt(e[2], 10));
      }
      return t;
    }
    static initOnDOMReady() {
      'loading' === document.readyState
        ? document.addEventListener('DOMContentLoaded', () => CountEverest.autoInit())
        : CountEverest.autoInit();
    }
    createDOM() {
      if ('' !== this.#e.innerHTML.trim()) return;
      const e = [],
        t = this.#t.units;
      this.#e.classList.contains('ce-countdown--theme-1')
        ? t.forEach((t) => {
            e.push(
              `<div class="ce-col"><span class="ce-${t}"></span> <span class="ce-${t}-label"></span></div>`
            );
          })
        : this.#e.classList.contains('ce-countdown--theme-6')
          ? t.forEach((t) => {
              e.push(
                `<div class="ce-col">\n          <div class="ce-${t}">\n            <div class="ce-flip-wrap"><div class="ce-flip-front"></div><div class="ce-flip-back"></div></div>\n          </div>\n          <span class="ce-${t}-label"></span>\n        </div>`
              );
            })
          : this.#e.classList.contains('ce-countdown--theme-9')
            ? t.forEach((t) => {
                e.push(
                  `<div class="ce-circle">\n          <div class="ce-circle__values">\n            <span class="ce-digit ce-${t}"></span><span class="ce-label ce-${t}-label"></span>\n          </div>\n        </div>`
                );
              })
            : this.#e.classList.contains('ce-countdown--theme-10')
              ? t.forEach((t) => {
                  e.push(
                    `<div class="ce-unit-wrap">\n          <div class="${t}"></div><span class="ce-${t}-label"></span>\n        </div>`
                  );
                })
              : this.#e.classList.contains('ce-countdown--theme-12')
                ? t.forEach((t) => {
                    e.push(
                      `<div class="ce-col">\n          <div class="ce-digits ce-${t}"></div>\n          <span class="ce-${t}-label"></span>\n        </div>`
                    );
                  })
                : t.forEach((t) => {
                    e.push(`<span class="ce-${t}"></span> <span class="ce-${t}-label"></span> `);
                  }),
        (this.#e.innerHTML = e.join('').trim());
    }
  }
  CountEverest.initOnDOMReady(), (window.CountEverest = CountEverest);
})();
