/*
    Elfsight Click To Call
    Version: 1.1.0
    Release date: Wed Sep 11 2019

    https://elfsight.com

    Copyright (c) 2019 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:"M16.81 12.494c-.699-.726-1.609-.726-2.338 0l-1.639 1.632c-.152.15-.274.18-.455.093-.332-.21-.73-.36-1.062-.575-1.58-1-2.916-2.271-4.1-3.718-.577-.726-1.09-1.48-1.458-2.358-.059-.18-.059-.273.093-.424.578-.517 1.09-1.057 1.64-1.603.757-.755.757-1.632 0-2.387L6.183 1.853C5.729 1.4 5.303.947 4.848.523c-.7-.697-1.61-.697-2.34 0L.87 2.155a2.841 2.841 0 0 0-.852 1.812C-.075 5.088.2 6.174.596 7.231c.822 2.208 2.065 4.113 3.552 5.896 2.036 2.422 4.462 4.327 7.29 5.686 1.278.604 2.585 1.086 3.979 1.18 1.003.057 1.855-.21 2.549-.97.455-.547 1.003-1 1.487-1.51.73-.756.73-1.662 0-2.388l-2.642-2.631z"})])};if(void 0===wp.components||void 0===wp.blocks||void 0===wp.element||void 0===wp.i18n)return!1;const{__:__}=wp.i18n,el=wp.element.createElement,registerBlockType=wp.blocks.registerBlockType,ServerSideRender=wp.components.ServerSideRender;let initTimeout;function initWidget(){clearTimeout(initTimeout),initTimeout=setTimeout(function(){let widgets=document.querySelectorAll("[data-elfsight-click-to-call-options]");Array.prototype.slice.call(widgets).forEach(function(widget){let options=widget.getAttribute("data-elfsight-click-to-call-options"),data=JSON.parse(decodeURIComponent(options));eval("eappsClickToCall(widget, data)"),widget.removeAttribute("data-elfsight-click-to-call-options"),widget.removeAttribute("data-elfsight-click-to-call-version"),widget.closest(".elfsight-block-widget-container").classList.add("elfsight-block-widget-initialized")})},1500)}async function getWidgets(){const e=await $.get(ajaxurl,{action:"elfsight-click-to-call".replace(/-/g,"_")+"_widgets_api",endpoint:"list"});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){if("1"===e.active)return t=e.id}),t}class Widget extends React.Component{componentDidMount(){initWidget()}componentDidUpdate(){initWidget()}render(){return this.props.id?el("div",{className:"elfsight-block-widget-container"},el(ServerSideRender,{block:"elfsight-click-to-call/block",attributes:{id:this.props.id||"1"}}),el("div",{className:"elfsight-block-widget-placeholder"},el(IconBlock,{}),el("span",{},"Elfsight Click To Call"))):[]}}class Button extends React.Component{render(){let e=document.location.origin+document.location.pathname.replace("post.php","admin.php")+"?page=elfsight-click-to-call#";return el("a",{href:e+this.props.href,target:"_blank",className:this.props.className},this.props.text)}}class WidgetSelect extends React.Component{constructor(){super(),this.state={widgets:[]}}setWidget(e){e.preventDefault();let t=e.target.querySelector("option:checked");this.props.setAttributes({id:t.value.toString()})}componentDidMount(){getWidgets().then(e=>{const[t,i]=e;this.setState({widgets:t});const l=this.props.id,s=!(!i[l]||"1"!==i[l].active);!s&&i?this.props.setAttributes({id:getWidgetId(t),exist:!0}):this.props.setAttributes({id:l,exist:s})})}render(){return this.state.widgets.length>0?el("div",{className:"components-base-control"},el("div",{className:"components-base-control__field"},el("select",{className:"components-select-control__input",id:"elfsight-click-to-call-block-control-id",value:this.props.id,onChange:this.setWidget.bind(this)},this.state.widgets.map(e=>wp.element.createElement("option",{value:e.id},[e.name]))))):[]}}registerBlockType("elfsight-click-to-call/block",{title:"Elfsight Click To Call",description:"Let your users reach you by phone in just one click.",icon:{src:IconBlock},category:"widgets",keywords:["Elfsight Click To Call","Elfsight"],supports:{html:!1},attributes:{id:{type:"number"},exist:{type:"bool",default:!1}},edit:function(e){const{attributes:{id:t,exist:i},setAttributes:l}=e;return getWidgets().then(e=>{const[s,o]=e;l(!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",{},"Elfsight Click To Call")),el("div",{className:"elfsight-block-form-text"},__("Let your users reach you by phone in just one click."),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);