/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{inherits as e,createSuper as t,classCallCheck as i,defineProperty as n,assertThisInitialized as o,createClass as r,get as a,getPrototypeOf as c,toConsumableArray as s}from"../../_virtual/_rollupPluginBabelHelpers.js";import l from"../../utils/util.js";import{COMPLETE as p,PIP_CHANGE as u}from"../../events.js";import{POSITIONS as d}from"../../plugin/plugin.js";import{xgIconTips as P}from"../common/iconTools.js";import h from"../common/iconPlugin.js";import m from"../assets/pipIcon.js";import f from"../assets/pipIconExit.js";var v="picture-in-picture",y="inline",I="fullscreen",g=/* @__PURE__ */function(){e(b,h);var g=t(b);function b(){var e;i(this,b);for(var t=arguments.length,r=new Array(t),a=0;a<t;a++)r[a]=arguments[a];return e=g.call.apply(g,[this].concat(r)),n(o(e),"switchPIP",function(t){if(!e.isPIPAvailable())return!1;t.stopPropagation&&t.stopPropagation(),e.isPip?(e.exitPIP(),e.emitUserAction(t,"change_pip",{props:"pip",from:!0,to:!1}),e.setAttr("data-state","normal")):4===e.player.media.readyState&&(e.requestPIP(),e.emitUserAction(t,"change_pip",{props:"pip",from:!1,to:!0}),e.setAttr("data-state","pip"))}),e}return r(b,[{key:"beforeCreate",value:function(e){"boolean"==typeof e.player.config.pip&&(e.config.showIcon=e.player.config.pip)}},{key:"afterCreate",value:function(){var e=this;this.isPIPAvailable()&&(a(c(b.prototype),"afterCreate",this).call(this),this.pMode=y,this.initPipEvents(),this.config.showIcon&&this.initIcons(),this.once(p,function(){e.config.showIcon&&(l.removeClass(e.find(".xgplayer-icon"),"xg-icon-disable"),e.bind("click",e.switchPIP))}))}},{key:"registerIcons",value:function(){return{pipIcon:{icon:m,class:"xg-get-pip"},pipIconExit:{icon:f,class:"xg-exit-pip"}}}},{key:"initIcons",value:function(){var e=this.icons;this.appendChild(".xgplayer-icon",e.pipIcon),this.appendChild(".xgplayer-icon",e.pipIconExit)}},{key:"initPipEvents",value:function(){var e=this,t=this.player;this.leavePIPCallback=function(){var i=t.paused;l.setTimeout(e,function(){!i&&t.mediaPlay()},0),!i&&t.mediaPlay(),e.setAttr("data-state","normal"),e.pipWindow=null,t.emit(u,!1)},this.enterPIPCallback=function(i){t.emit(u,!0),null!=i&&i.pictureInPictureWindow&&(e.pipWindow=i.pictureInPictureWindow),e.setAttr("data-state","pip")},this.onWebkitpresentationmodechanged=function(i){var n=t.media.webkitPresentationMode;e.pMode===I&&n!==I&&t.onFullscreenChange(null,!1),e.pMode=n,n===v?e.enterPIPCallback(i):n===y&&e.leavePIPCallback(i)},t.media&&(t.media.addEventListener("enterpictureinpicture",this.enterPIPCallback),t.media.addEventListener("leavepictureinpicture",this.leavePIPCallback),b.checkWebkitSetPresentationMode(t.media)&&t.media.addEventListener("webkitpresentationmodechanged",this.onWebkitpresentationmodechanged))}},{key:"copyStyleIntoPiPWindow",value:function(e){var t=s(document.styleSheets).map(function(t){try{return s(t.cssRules).map(function(e){return e.cssText}).join("")}catch(n){var i=document.createElement("link");i.rel="stylesheet",i.type=t.type,i.media=t.media,i.href=t.href,e.document.head.appendChild(i)}return""}).filter(Boolean).join("\n"),i=document.createElement("style");i.textContent=t,e.document.head.appendChild(i)}},{key:"requestPIP",value:function(){var e=this,t=this.player,i=this.playerConfig,n=this.config;if(this.isPIPAvailable()&&!this.isPip)try{var o=i.poster;if(o&&(t.media.poster="String"===l.typeOf(o)?o:o.poster),n.preferDocument&&this.isDocPIPAvailable()){var r={};if(n.width&&n.height)r.width=n.width,r.height=n.height;else{var a=t.root.getBoundingClientRect();r.width=a.width,r.height=a.height}documentPictureInPicture.requestWindow(r).then(function(i){var o=n.docPiPNode,r=n.docPiPStyle;e.enterPIPCallback();var a=o||t.root,c=a.parentElement,s=a.previousSibling,l=a.nextSibling;e.copyStyleIntoPiPWindow(i);var p=document.createElement("style");if(p.append("body{padding:0; margin:0;}"),r){var u="";"string"==typeof r?u=r:"function"==typeof r&&(u=r.call(n)),u&&p.append(u)}else a===t.root&&p.append("\n              .xgplayer{width: 100%!important; height: 100%!important;}\n            ");i.document.head.append(p),i.document.body.append(a),i.addEventListener("pagehide",function(t){c&&(l?c.insertBefore(a,l):s?c.insertBefore(a,s.nextSibling):c.appendChild(a)),e.leavePIPCallback()},{once:!0})})}else b.checkWebkitSetPresentationMode(t.media)?t.media.webkitSetPresentationMode("picture-in-picture"):t.media.requestPictureInPicture();return!0}catch(c){return console.error("requestPiP",c),!1}}},{key:"exitPIP",value:function(){var e=this.player;try{var t;if(this.isPIPAvailable()&&this.isPip)this.isDocPIPAvailable()&&null!==(t=documentPictureInPicture)&&void 0!==t&&t.window?documentPictureInPicture.window.close():b.checkWebkitSetPresentationMode(e.media)?e.media.webkitSetPresentationMode("inline"):document.exitPictureInPicture();return!0}catch(i){return console.error("exitPIP",i),!1}}},{key:"isPip",get:function(){var e,t=this.player;return!(!this.isDocPIPAvailable()||null===(e=documentPictureInPicture)||void 0===e||!e.window)||document.pictureInPictureElement&&document.pictureInPictureElement===t.media||t.media.webkitPresentationMode===v}},{key:"isPIPAvailable",value:function(){var e=this.player.media;return"Boolean"===l.typeOf(document.pictureInPictureEnabled)&&document.pictureInPictureEnabled&&("Boolean"===l.typeOf(e.disablePictureInPicture)&&!e.disablePictureInPicture||e.webkitSupportsPresentationMode&&"Function"===l.typeOf(e.webkitSetPresentationMode))||this.isDocPIPAvailable()}},{key:"isDocPIPAvailable",value:function(){return"documentPictureInPicture"in window&&/^(https|file)/.test(location.protocol)}},{key:"destroy",value:function(){a(c(b.prototype),"destroy",this).call(this);var e=this.player;e.media.removeEventListener("enterpictureinpicture",this.enterPIPCallback),e.media.removeEventListener("leavepictureinpicture",this.leavePIPCallback),b.checkWebkitSetPresentationMode(e.media)&&e.media.removeEventListener("webkitpresentationmodechanged",this.onWebkitpresentationmodechanged),this.exitPIP(),this.unbind("click",this.btnClick)}},{key:"render",value:function(){if(this.config.showIcon&&this.isPIPAvailable())return'<xg-icon class="xgplayer-pip">\n      <div class="xgplayer-icon xg-icon-disable">\n      </div>\n      '.concat(P(this,"PIP",this.playerConfig.isHideTips),"\n    </xg-icon>")}}],[{key:"pluginName",get:function(){return"pip"}},{key:"defaultConfig",get:function(){return{position:d.CONTROLS_RIGHT,index:6,showIcon:!1,preferDocument:!1,width:void 0,height:void 0,docPiPNode:void 0,docPiPStyle:void 0}}},{key:"checkWebkitSetPresentationMode",value:function(e){return"function"==typeof e.webkitSetPresentationMode}}]),b}();export{g as default};