/*
    File Embed
    Version: 1.1.1
    Release date: Fri Dec 25 2020

    https://elfsight.com

    Copyright (c) 2020 Elfsight, LLC. ALL RIGHTS RESERVED
*/

!function(wp,$){"use strict";let IconBlock=function(e){return wp.element.createElement("svg",{width:"20",height:"20",viewBox:"0 0 20 20",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink",class:"dashicon"},[wp.element.createElement("path",{d:"M3.26 1.972v16.056c0 .156.128.282.286.282h12.908a.284.284 0 0 0 .287-.282V6.072a.806.806 0 0 0-.81-.806l-1.574.008a1.613 1.613 0 0 1-1.62-1.613V2.497a.806.806 0 0 0-.807-.807H3.546a.284.284 0 0 0-.287.282zM12.736 0a3.19 3.19 0 0 1 2.053.746l2.57 2.163a3.076 3.076 0 0 1 1.102 2.353v12.766c0 1.09-.9 1.972-2.008 1.972H3.546c-1.109 0-2.008-.883-2.008-1.972V1.972C1.538.882 2.438 0 3.546 0h9.19zm-7.34 8.042c.063-.61.353-1.157.817-1.542l.267-.221c.396-.329.895-.51 1.406-.51.68 0 1.315.31 1.741.85l4.104 5.196c.376.475.549 1.073.486 1.682a2.293 2.293 0 0 1-.818 1.541l-.267.222a2.208 2.208 0 0 1-1.642.496 2.207 2.207 0 0 1-1.496-.826l-2.769-3.505a1.482 1.482 0 0 1 .21-2.047l.14-.116a1.402 1.402 0 0 1 1.999.216l2.764 3.499a.543.543 0 0 1-.078.75.514.514 0 0 1-.732-.079l-2.764-3.499a.375.375 0 0 0-.533-.057l-.14.116a.392.392 0 0 0-.056.547l2.76 3.494a1.182 1.182 0 0 0 1.682.181l.267-.22c.249-.207.404-.5.437-.825a1.238 1.238 0 0 0-.26-.9L8.819 7.29a1.182 1.182 0 0 0-1.682-.182l-.267.221a1.247 1.247 0 0 0-.177 1.724.539.539 0 0 1-.078.75.514.514 0 0 1-.732-.079 2.315 2.315 0 0 1-.485-1.682z",id:"a"})])};if(void 0===wp.components||void 0===wp.blocks||void 0===wp.element||void 0===wp.i18n)return!1;const{Component:Component}=window.React,{__:__}=wp.i18n,el=wp.element.createElement,registerBlockType=wp.blocks.registerBlockType,ServerSideRender=wp.components.ServerSideRender,restApiUrl=window.wpApiSettings.root+"/elfsight-file-embed/admin",restApiNonce=window.wpApiSettings.nonce;let initTimeout;function initWidget(){clearTimeout(initTimeout),initTimeout=setTimeout(function(){const widgets=document.querySelectorAll("[data-elfsight-file-embed-options]");Array.prototype.slice.call(widgets).forEach(function(widget){const options=widget.getAttribute("data-elfsight-file-embed-options"),data=JSON.parse(decodeURIComponent(options));eval("eappsFileEmbed(widget, data)"),widget.removeAttribute("data-elfsight-file-embed-options"),widget.removeAttribute("data-elfsight-file-embed-version"),widget.closest(".elfsight-block-widget-container").classList.add("elfsight-block-widget-initialized")})},1500)}async function getWidgets(){const e=await $.ajax({type:"GET",url:restApiUrl+"/widgets/list/",beforeSend:function(e){e.setRequestHeader("X-WP-Nonce",restApiNonce)}});return e.status?[e.data,e.data.reduce(function(e,t){return e[t.id]=t,e},{})]:[]}function getWidgetId(e){let t;return e.some(function(e){return"1"===e.active&&(t=parseInt(e.id),!0)}),t}class Widget extends Component{componentDidMount(){initWidget()}componentDidUpdate(){initWidget()}render(){const{id:e}=this.props;return e?el("div",{className:"elfsight-block-widget-container"},el(ServerSideRender,{block:"elfsight-file-embed/block",attributes:{id:e}}),el("div",{className:"elfsight-block-widget-placeholder"},el(IconBlock,{}),el("span",{},"File Embed"))):null}}class Button extends Component{render(){const{href:e,className:t,text:i}=this.props,n=document.location.origin+document.location.pathname.replace("post.php","admin.php")+"?page=elfsight-file-embed#";return el("a",{href:n+e,target:"_blank",className:t},i)}}class WidgetSelect extends Component{constructor(){super(),this.state={widgets:[]}}setWidget(e){e.preventDefault();const{setAttributes:t}=this.props,i=e.target.querySelector("option:checked");t({id:parseInt(i.value)})}componentDidMount(){const{id:e,setAttributes:t}=this.props;getWidgets().then(i=>{const[n,s]=i;this.setState({widgets:n});const o=!(!s[e]||"1"!==s[e].active);t(!o&&s?{id:getWidgetId(n),exist:!0}:{id:e,exist:o})})}render(){const{widgets:e}=this.state,{id:t}=this.props;return e.length>0?el("div",{className:"components-base-control"},el("div",{className:"components-base-control__field"},el("select",{className:"components-select-control__input",id:"elfsight-file-embed-block-control-id",value:t,onChange:this.setWidget.bind(this)},e.map(({id:e,name:t})=>el("option",{value:e},t))))):null}}registerBlockType("elfsight-file-embed/block",{title:"File Embed",description:"Integrate 20 types of files into your web pages to enable viewing and downloading.",icon:{src:IconBlock},category:"widgets",keywords:["File Embed","Elfsight"],supports:{html:!1},attributes:{id:{type:"number"},exist:{type:"bool",default:!1}},edit:function(e){const{attributes:{id:t,exist:i},setAttributes:n}=e;return getWidgets().then(e=>{const[s,o]=e;n(!i&&s?{id:getWidgetId(s),exist:!0}:{id:t,exist:i})}),el(wp.element.Fragment,{},el(wp.editor.InspectorControls,{},el(wp.components.PanelBody,{className:"elfsight-block-panel",title:"Select widget"},el(WidgetSelect,{id:t,setAttributes:function(t){e.setAttributes(t)}}),i?el("div",{className:"elfsight-block-panel-group"},el(Button,{href:"/edit-widget/"+t,className:"components-button is-button is-default is-large elfsight-block-panel-button",text:__("Edit Widget")}),el(Button,{href:"/add-widget/",className:"elfsight-block-panel-link",text:__("Create new widget")})):el("div",{className:"elfsight-block-panel-group"},el("span",{},__("No widgets yet")),el(Button,{href:"/add-widget/",className:"components-button is-button is-default is-primary is-large elfsight-block-panel-button",text:__("Create Widget")})))),i?el(Widget,{id:t,exist:i}):null,i?null:el("div",{className:"elfsight-block-form"},el("div",{className:"elfsight-block-form-header"},el(IconBlock,{}),el("span",{},"File Embed")),el("div",{className:"elfsight-block-form-text"},__("Integrate 20 types of files into your web pages to enable viewing and downloading."),el("br"),el("strong",{},__("Let's create your first widget!"))),el(Button,{href:"/add-widget/",className:"components-button is-button is-default is-primary is-large elfsight-block-form-button",text:__("Create Widget")})))},save:function(){return null}})}(wp,jQuery);