(()=>{function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function r(e){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?t(Object(o),!0).forEach((function(t){n(e,t,o[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))}))}return e}function n(t,r,n){return(r=function(t){var r=function(t){if("object"!=e(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,"string");if("object"!=e(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==e(r)?r:r+""}(r))in t?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n,t}!function(e){"use strict";var t={editProductName:function(){jQuery(".products_container, .products_before_container, .products_after_container").on("click",".edit_item_name",(function(){var t=e(this).closest("td").find(".item_input_name"),r=e(this).closest("td").find("select"),n=e(this).closest("td").find(".product_select_name"),o=e(this).closest("td").find(".select-product");if(t.length)r.attr("name","product[name][]"),o.show(),t.remove();else{r.attr("name","product_name"),o.hide();var i=r.find("option:first-child").val();n.append('<input type="text" class="item_input_name" name="product[name][]" value="'+i+'" /> '),n.find(".item_input_name").focus()}return!1}))},initProductsSelect2:function(t){var n=this,o={placeholder:inspire_invoice_params.select2_placeholder,language:{inputTooShort:function(e){var t=e.minimum-e.input.length;return inspire_invoice_params.select2_min_chars.replace("%",t)},loadingMore:function(){return inspire_invoice_params.select2_loading_more},noResults:function(){return inspire_invoice_params.select2_no_results},searching:function(){return inspire_invoice_params.select2_searching},errorLoading:function(){return inspire_invoice_params.select2_error_loading}}};t.length&&t.select2(r(r({ajax:{url:ajaxurl,dataType:"json",delay:300,type:"POST",data:function(e){return{action:"fiw_find_products",name:e.term,security:fiw_localize.nonce}},processResults:function(e){return{results:e.items}},cache:!0},minimumInputLength:3},o),{},{width:"100%"})),t.on("select2:select",(function(t){var r=t.params.data,o=e(this).parents(".product_row");o[0].querySelector("input[name='product[net_price][]']").value=r.net_price,o[0].querySelector("input[name='product[net_price_sum][]']").value=r.net_price,o[0].querySelector("input[name='product[vat_sum][]']").value=r.tax_amount,o[0].querySelector("input[name='product[sku][]']").value=r.sku,o[0].querySelector("input[name='product[quantity][]']").value=1,o[0].querySelector("input[name='product[unit][]']").value="",o[0].querySelector("input[name='product[total_price][]']").value=r.gross_price;var i=n.get_option_value(o[0].querySelector("select[name='product[vat_type][]']").options,r.tax_rate);o[0].querySelector("select[name='product[vat_type][]']").value=i,n.invoiceRefreshTotal()}))},invoiceRefreshTotal:function(){var t=0;e(".product_row .total_price").each((function(r,n){var o=e(n).val(),i=parseFloat(o.replace(",","."));t+=isNaN(i)?0:i})),e("[name=total_price]").val(t.toFixed(2))},get_option_value:function(e,t){for(var r=e[0].value,n=0;n<e.length;n++)if(t==e[n].value.split("|")[1])return e[n].value;return r}};jQuery("#products").on("click",".add_document_product",(function(e){var r=jQuery(".products_metabox").find(".refresh_product");t.initProductsSelect2(r)})),jQuery(".products_metabox").length&&jQuery(".refresh_product").each((function(){t.initProductsSelect2(e(this))})),t.editProductName()}(jQuery)})();