/*
    Instagram Feed
    Version: 4.0.1
    Release date: Tue Sep 22 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",{xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink",width:"20",height:"20",viewBox:"0 0 20 20",class:"dashicon"},[wp.element.createElement("path",{id:"a",d:"M12.58 0a4.103 4.103 0 0 1 4.092 4.092v8.488a4.103 4.103 0 0 1-4.092 4.092H4.092A4.103 4.103 0 0 1 0 12.58V4.092A4.103 4.103 0 0 1 4.092 0h8.488zm2.37 12.387h-.009V4.286a2.564 2.564 0 0 0-2.554-2.555H4.286A2.564 2.564 0 0 0 1.73 4.286v8.1a2.564 2.564 0 0 0 2.555 2.555h8.109a2.564 2.564 0 0 0 2.555-2.554zm3.319-8.303A4.09 4.09 0 0 1 20 7.42v8.488A4.103 4.103 0 0 1 15.908 20H7.42c-1.37 0-2.596-.69-3.336-1.731h11.63a2.564 2.564 0 0 0 2.555-2.555V4.084zM8.244 4.067a4.266 4.266 0 0 1 4.26 4.26 4.271 4.271 0 0 1-4.26 4.261 4.271 4.271 0 0 1-4.26-4.26 4.266 4.266 0 0 1 4.26-4.26zm0 6.698a2.435 2.435 0 0 0 2.428-2.429 2.435 2.435 0 0 0-2.428-2.428 2.435 2.435 0 0 0-2.429 2.428 2.435 2.435 0 0 0 2.429 2.429zM12.513 5a.941.941 0 1 1 0-1.882.941.941 0 0 1 0 1.882z"})])};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-instagram-feed/admin",restApiNonce=window.wpApiSettings.nonce;let initTimeout;function initWidget(){clearTimeout(initTimeout),initTimeout=setTimeout(function(){const widgets=document.querySelectorAll("[data-elfsight-instagram-feed-options]");Array.prototype.slice.call(widgets).forEach(function(widget){const options=widget.getAttribute("data-elfsight-instagram-feed-options"),data=JSON.parse(decodeURIComponent(options));eval("eappsInstagramFeed(widget, data)"),widget.removeAttribute("data-elfsight-instagram-feed-options"),widget.removeAttribute("data-elfsight-instagram-feed-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-instagram-feed/block",attributes:{id:e}}),el("div",{className:"elfsight-block-widget-placeholder"},el(IconBlock,{}),el("span",{},"Instagram Feed"))):null}}class Button extends Component{render(){const{href:e,className:t,text:i}=this.props,s=document.location.origin+document.location.pathname.replace("post.php","admin.php")+"?page=elfsight-instagram-feed#";return el("a",{href:s+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[s,n]=i;this.setState({widgets:s});const o=!(!n[e]||"1"!==n[e].active);t(!o&&n?{id:getWidgetId(s),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-instagram-feed-block-control-id",value:t,onChange:this.setWidget.bind(this)},e.map(({id:e,name:t})=>el("option",{value:e},t))))):null}}registerBlockType("elfsight-instagram-feed/block",{title:"Instagram Feed",description:"Add Instagram images to your website to engage your visitors",icon:{src:IconBlock},category:"widgets",keywords:["Instagram Feed","Elfsight"],supports:{html:!1},attributes:{id:{type:"number"},exist:{type:"bool",default:!1}},edit:function(e){const{attributes:{id:t,exist:i},setAttributes:s}=e;return getWidgets().then(e=>{const[n,o]=e;s(!i&&n?{id:getWidgetId(n),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",{},"Instagram Feed")),el("div",{className:"elfsight-block-form-text"},__("Add Instagram images to your website to engage your visitors"),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);