'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-b499856a.js');

const VideoPlayer = {
  HTML5Video: 'html',
  Vimeo: 'vimeo',
};
Object.values(VideoPlayer);

const teliaVideoPlayerCss = ":host{display:block;height:inherit;width:inherit}:host video{height:inherit;object-fit:cover;object-position:center;width:inherit}:host .telia-video-player__container{display:block;height:inherit;width:inherit}:host .telia-video-player__container iframe{height:100%;width:100%}";

const TeliaVideoPlayer = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    /** Tracking id is used to identify and count Component usage */
    this.dataTrackingId = 'telia-video-player';
    this.setVideoRef = (element) => {
      this.videoElement = element;
    };
  }
  /**
   * Play html video.
   */
  async play() {
    var _a;
    if (typeof ((_a = this.videoElement) === null || _a === void 0 ? void 0 : _a.play) === 'function') {
      this.videoElement.play();
    }
  }
  /**
   * Pause html video.
   */
  async pause() {
    var _a;
    if (typeof ((_a = this.videoElement) === null || _a === void 0 ? void 0 : _a.pause) === 'function') {
      this.videoElement.pause();
    }
  }
  /**
   * Validate and return optional subtitles
   * @private
   * @returns `Array<VideoSubtitles>`
   */
  getHTML5VideoSubtitles() {
    if (Array.isArray(this.subtitles)) {
      return this.subtitles;
    }
    try {
      return this.subtitles && JSON.parse(this.subtitles);
    }
    catch (e) {
      console.error('Telia Video Player: please provide a valid subtitles JSON string');
      return undefined;
    }
  }
  /**
   * Validate, compose and return vimeo source with query parameters as settings
   * @private
   * @returns `string`
   */
  getActiveVimeoSourceSettings() {
    var _a;
    let videoSource = this.src;
    if ((_a = this.src) === null || _a === void 0 ? void 0 : _a.includes('https://player.vimeo.com/video/')) {
      const vimeoUrl = new URL(this.src);
      const predefinedParams = new URLSearchParams(vimeoUrl.searchParams);
      let vimeoParams = new URLSearchParams();
      // h param needs to be included if the vimeo video is private
      if (predefinedParams.get('h')) {
        vimeoParams.append('h', predefinedParams.get('h'));
      }
      // remove vimeo video and account details
      this.applyVimeoParamSettings(vimeoParams, 'title', false);
      this.applyVimeoParamSettings(vimeoParams, 'byline', false);
      this.applyVimeoParamSettings(vimeoParams, 'portrait', false);
      this.applyVimeoParamSettings(vimeoParams, 'background', !this.controls);
      this.applyVimeoParamSettings(vimeoParams, 'autoplay', this.autoplay);
      this.applyVimeoParamSettings(vimeoParams, 'loop', this.loop);
      this.applyVimeoParamSettings(vimeoParams, 'muted', this.muted);
      videoSource = `${vimeoUrl.origin}${vimeoUrl.pathname}?${vimeoParams.toString()}`;
    }
    else {
      videoSource = undefined;
    }
    return videoSource;
  }
  applyVimeoParamSettings(params, name, value) {
    params.append(name, value ? '1' : '0');
  }
  render() {
    var _a;
    const componentClass = 'telia-video-player';
    return (index.h(index.Host, { class: componentClass, "data-testid": this.dataTestid }, index.h("telia-vst", { dataTrackingId: this.dataTrackingId }), this.type === VideoPlayer.HTML5Video && (index.h("video", { autoplay: this.autoplay ? true : null, controls: this.controls, muted: this.muted, loop: this.loop, poster: this.poster, ref: this.setVideoRef, preload: this.preload, playsinline: true,
      //@ts-ignore
      controlsList: "nodownload nofullscreen noremoteplayback noplaybackrate", disablepictureinpicture: true }, index.h("source", { src: this.src, type: this.srcType }), (_a = this.getHTML5VideoSubtitles()) === null || _a === void 0 ? void 0 :
      _a.map(list => (index.h("track", { label: list.label, kind: "subtitles", srclang: list.srcLanguage, src: list.src, default: list.default }))))), this.type === VideoPlayer.Vimeo && (index.h("div", { class: componentClass + '__container' }, this.getActiveVimeoSourceSettings() ? (index.h("iframe", { title: this.iframeTitle, src: this.getActiveVimeoSourceSettings(), frameborder: "0", allow: "autoplay; fullscreen;" })) : (this.poster && index.h("telia-image", { src: this.poster }))))));
  }
};
TeliaVideoPlayer.style = teliaVideoPlayerCss;

exports.telia_video_player = TeliaVideoPlayer;
