!function(e,t,r){var a=function(t){e(t).agselect2({placeholder:AgRules.search,ajax:{url:ajaxurl,dataType:"json",method:"post",delay:250,data:function(e){return{q:e.term,page:e.page,action:"ag_theme_builder_get_posts_by_query",nonce:AgRules.ajax_nonce}},processResults:function(e){return{results:e}},cache:!0},minimumInputLength:2,language:AgRules.ag_lang})},n=function(t){var r=t.find(".ag-target_rule-input");r.val();var a=[];t.find(".ag-target-rule-condition").each(function(t){var r=e(this),n={},o=r.find("select.target_rule-condition"),i=r.find("select.target_rule-specific-page"),l=o.val(),p=i.val();""!=l&&(n={type:l,specific:p},a.push(n))});var n=JSON.stringify(a);r.val(n)},o=function(e){type=e.closest(".ag-target-rule-wrapper").attr("data-type"),rules=e.find(".ag-target-rule-condition"),show_close=!1,"display"==type?rules.length>1&&(show_close=!0):show_close=!0,rules.each(function(){show_close?jQuery(this).find(".target_rule-condition-delete").removeClass("ag-hidden"):jQuery(this).find(".target_rule-condition-delete").addClass("ag-hidden")})},i=function(t,r){var a=e(".ag-target-rule-display-on-wrap"),n=e(".ag-target-rule-exclude-on-wrap"),o=n.closest("tr"),i=a.find(".target_rule-add-exclusion-rule"),l=n.find(".ag-target-rule-condition");!0==r?(o.addClass("ag-hidden"),i.removeClass("ag-hidden")):!0==t?(o.removeClass("ag-hidden"),i.addClass("ag-hidden")):1==l.length&&""==e(l[0]).find("select.target_rule-condition").val()?(o.addClass("ag-hidden"),i.removeClass("ag-hidden")):(o.removeClass("ag-hidden"),i.addClass("ag-hidden"))};e(document).ready(function(e){jQuery(".ag-target-rule-condition").each(function(){var t=e(this),r=t.find("select.target_rule-condition").val(),a=t.next(".target_rule-specific-page-wrap");"specifics"==r&&a.slideDown(300)}),jQuery("select.target-rule-select2").each(function(e,t){a(t)}),jQuery(".ag-target-rule-selector-wrapper").each(function(){o(jQuery(this))}),i(),jQuery(document).on("change",".ag-target-rule-condition select.target_rule-condition",function(e){var t=jQuery(this),r=t.val(),a=t.closest(".ag-target-rule-wrapper");"specifics"==r?t.closest(".ag-target-rule-condition").next(".target_rule-specific-page-wrap").slideDown(300):t.closest(".ag-target-rule-condition").next(".target_rule-specific-page-wrap").slideUp(300),n(a)}),jQuery(".ag-target-rule-selector-wrapper").on("change",".target-rule-select2",function(e){n(jQuery(this).closest(".ag-target-rule-wrapper"))}),jQuery(".ag-target-rule-selector-wrapper").on("click",".target_rule-add-rule-wrap a",function(e){e.preventDefault(),e.stopPropagation();var t=jQuery(this),r=parseInt(t.attr("data-rule-id"))+1,n=t.attr("data-rule-type"),i=t.closest(".ag-target-rule-selector-wrapper").find(".target_rule-builder-wrap"),l=wp.template("ag-target-rule-"+n+"-condition"),p=t.closest(".ag-target-rule-wrapper");i.append(l({id:r,type:n})),a(".ag-target-rule-"+n+"-on .target-rule-select2"),t.attr("data-rule-id",r),o(p)}),jQuery(".ag-target-rule-selector-wrapper").on("click",".target_rule-condition-delete",function(e){var t=jQuery(this),r=t.closest(".ag-target-rule-condition"),a=t.closest(".ag-target-rule-wrapper");cnt=0,data_type=a.attr("data-type"),optionVal=t.siblings(".target_rule-condition-wrap").children(".target_rule-condition").val(),"exclude"==data_type&&1===a.find(".target_rule-condition").length?(a.find(".target_rule-condition").val(""),a.find(".target_rule-specific-page").val(""),a.find(".target_rule-condition").trigger("change"),i(!1,!0)):(t.parent(".ag-target-rule-condition").next(".target_rule-specific-page-wrap").remove(),r.remove()),a.find(".ag-target-rule-condition").each(function(e){var t=jQuery(this),r=t.attr("data-rule"),a=t.find(".target_rule-condition"),n=(t.find(".target_rule-specific-page"),a.attr("name"));t.attr("data-rule",e),a.attr("name",n.replace("["+r+"]","["+e+"]")),t.removeClass("ag-target-rule-"+r).addClass("ag-target-rule-"+e),cnt=e}),a.find(".target_rule-add-rule-wrap a").attr("data-rule-id",cnt),o(a),n(a)}),jQuery(".ag-target-rule-selector-wrapper").on("click",".target_rule-add-exclusion-rule a",function(e){e.preventDefault(),e.stopPropagation(),i(!0)})})}(jQuery,window);