(function (ns) {
  'use strict';

  const Patterns = Object.freeze({
    MICRO: 50,
    MINI: 100,
    SHORT: 200,
    LONG: 2000,
    ALARM: Object.freeze([100, 50, 100]),
    POI: Object.freeze([2000, 200, 2000]),
    ACTIVE_POI: Object.freeze([500, 300, 500])
  });

  function supported() {
    return typeof navigator !== 'undefined' && typeof navigator.vibrate === 'function';
  }

  function normalize(pattern) {
    const selected = pattern ?? Patterns.LONG;

    if (Array.isArray(selected)) {
      const values = selected.map(Number);
      if (!values.length || values.some(value => !Number.isFinite(value) || value < 0)) {
        throw new TypeError('Das Vibrationsmuster muss aus nichtnegativen Millisekunden bestehen.');
      }
      return values;
    }

    const duration = Number(selected);
    if (!Number.isFinite(duration) || duration < 0) {
      throw new TypeError('Die Vibrationsdauer muss eine nichtnegative Millisekundenangabe sein.');
    }
    return [duration];
  }

  function start(pattern = Patterns.LONG) {
    const normalized = normalize(pattern);
    if (!supported()) return false;
    return navigator.vibrate(normalized);
  }

  function stop() {
    if (!supported()) return false;
    return navigator.vibrate(0);
  }

  ns.Vibration = Object.freeze({
    Patterns,
    isSupported: supported,
    start,
    stop
  });
}(globalThis.Wegwichtel));
