"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.LinkedProductListBlockEdit=exports.EmptyStateImage=void 0;const element_1=require("@wordpress/element"),block_templates_1=require("@woocommerce/block-templates"),components_1=require("@wordpress/components"),i18n_1=require("@wordpress/i18n"),icons_1=require("@wordpress/icons"),tracks_1=require("@woocommerce/tracks"),core_data_1=require("@wordpress/core-data"),use_product_entity_prop_1=__importDefault(require("../../../hooks/use-product-entity-prop")),product_list_1=require("../../../components/product-list"),product_select_1=require("../../../components/product-select"),advice_card_1=require("../../../components/advice-card"),constants_1=require("../../../constants"),shopping_bags_1=require("../../../images/shopping-bags"),cash_register_1=require("../../../images/cash-register"),reducer_1=require("./reducer"),get_related_products_1=require("../../../utils/get-related-products"),block_slot_fill_1=require("../../../components/block-slot-fill");function EmptyStateImage({image:e,tip:t}){switch(e){case"CashRegister":return(0,element_1.createElement)(cash_register_1.CashRegister,null);case"ShoppingBags":return(0,element_1.createElement)(shopping_bags_1.ShoppingBags,null);default:return/^https?:\/\//.test(e)?(0,element_1.createElement)("img",{src:e,alt:t,height:88,width:88}):null}}function LinkedProductListBlockEdit({attributes:e,context:{postType:t}}){const{property:r,emptyState:c}=e,o=(0,block_templates_1.useWooBlockProps)(e),[s,i]=(0,element_1.useReducer)(reducer_1.reducer,{linkedProducts:[],searchedProducts:[]}),d=(0,core_data_1.useEntityId)("postType",t),n=(0,reducer_1.getLoadLinkedProductsDispatcher)(i),_=(0,reducer_1.getSearchProductsDispatcher)(i),l=(0,reducer_1.getSelectSearchedProductDispatcher)(i),u=(0,reducer_1.getRemoveLinkedProductDispatcher)(i),[a,p]=(0,use_product_entity_prop_1.default)(r,{postType:t});(0,element_1.useEffect)((()=>{s.selectedProduct||n(null!=a?a:[])}),[a,s.selectedProduct]);const m=(0,element_1.useCallback)(((e="")=>{const t=[d];return a&&t.push(...a),_(t,e)}),[a]);(0,element_1.useEffect)((()=>{m()}),[m]);const[k,g]=(0,element_1.useState)(!1);return(0,element_1.createElement)("div",{...o},(0,element_1.createElement)(block_slot_fill_1.SectionActions,null,(0,element_1.createElement)(components_1.Button,{variant:"tertiary",icon:icons_1.reusableBlock,onClick:async function(){(0,tracks_1.recordEvent)("linked_products_choose_related_click",{source:constants_1.TRACKS_SOURCE,field:r}),i({type:"LOADING_LINKED_PRODUCTS",payload:{isLoading:!0}}),g(!0);const e=await(0,get_related_products_1.getSuggestedProductsFor)({postId:d,forceRequest:!0});if(i({type:"LOADING_LINKED_PRODUCTS",payload:{isLoading:!1}}),g(!1),!e)return;const t=l(e,[]);p(t)},isBusy:k,disabled:k},(0,i18n_1.__)("Choose products for me","woocommerce"))),(0,element_1.createElement)("div",{className:"wp-block-woocommerce-product-linked-list-field__form-group-content"},(0,element_1.createElement)(product_select_1.ProductSelect,{items:s.searchedProducts,selected:null,filter:m,onSelect:function(e){const t=l(e,s.linkedProducts);p(t),(0,tracks_1.recordEvent)("linked_products_product_add",{source:constants_1.TRACKS_SOURCE,field:r,product_id:d,linked_product_id:e.id})}})),s.isLoading&&(0,element_1.createElement)(product_list_1.Skeleton,null),!s.isLoading&&0===s.linkedProducts.length&&(0,element_1.createElement)(advice_card_1.AdviceCard,{tip:c.tip,dismissPreferenceId:`woocommerce-product-${r}-advice-card-dismissed`,isDismissible:c.isDismissible,onDismiss:function(){(0,tracks_1.recordEvent)("linked_products_placeholder_dismiss",{source:constants_1.TRACKS_SOURCE,field:r})}},(0,element_1.createElement)(EmptyStateImage,{...c})),!s.isLoading&&s.linkedProducts.length>0&&(0,element_1.createElement)(product_list_1.ProductList,{products:s.linkedProducts,onRemove:function(e){const t=u(e,s.linkedProducts);p(t),(0,tracks_1.recordEvent)("linked_products_product_remove",{source:constants_1.TRACKS_SOURCE,field:r,product_id:d,linked_product_id:e.id})},onEdit:function(e){(0,tracks_1.recordEvent)("linked_products_product_select",{source:constants_1.TRACKS_SOURCE,field:r,product_id:d,linked_product_id:e.id})},onPreview:function(e){(0,tracks_1.recordEvent)("linked_products_product_preview_click",{source:constants_1.TRACKS_SOURCE,field:r,product_id:d,linked_product_id:e.id})}}))}exports.EmptyStateImage=EmptyStateImage,exports.LinkedProductListBlockEdit=LinkedProductListBlockEdit;