"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ImageBlockEdit=void 0;const i18n_1=require("@wordpress/i18n"),components_1=require("@wordpress/components"),classnames_1=__importDefault(require("classnames")),element_1=require("@wordpress/element"),icons_1=require("@wordpress/icons"),block_templates_1=require("@woocommerce/block-templates"),components_2=require("@woocommerce/components"),tracks_1=require("@woocommerce/tracks"),core_data_1=require("@wordpress/core-data"),place_holder_1=require("./place-holder"),block_slot_fill_1=require("../../../components/block-slot-fill");function mapUploadImageToImage(e){return e.id?{id:e.id,name:e.title,src:e.url,alt:e.alt}:null}function ImageBlockEdit({attributes:e,context:r}){var o;const{property:t,multiple:a}=e,[l,n]=(0,core_data_1.useEntityProp)("postType",r.postType,t),[i,c]=(0,element_1.useState)(!1),[m,s]=(0,element_1.useState)(!1),[_,d]=(0,element_1.useState)(null),p=(0,block_templates_1.useWooBlockProps)(e,{className:(0,classnames_1.default)({"has-images":Array.isArray(l)?l.length>0:Boolean(l)})});function u(e){return function(r){var o;if((0,tracks_1.recordEvent)(e),Array.isArray(r)){const e=r.filter((e=>e.id)).map((e=>({id:e.id,name:e.title,src:e.url,alt:e.alt})));(null===(o=r[0])||void 0===o?void 0:o.id)&&n([...l,...e])}else r.id&&n(mapUploadImageToImage(r))}}const g=null!==l&&(!Array.isArray(l)||l.length>0);return(0,element_1.createElement)("div",{...p},(0,element_1.createElement)("div",{className:"woocommerce-product-form__image-drop-zone"},i?(0,element_1.createElement)("div",{className:"woocommerce-product-form__remove-image-drop-zone"},(0,element_1.createElement)("span",null,(0,element_1.createElement)(icons_1.Icon,{icon:icons_1.trash,size:20,className:"icon-control"}),(0,i18n_1.__)("Drop here to remove","woocommerce")),(0,element_1.createElement)(components_1.DropZone,{onHTMLDrop:()=>s(!0),onDrop:()=>s(!0),label:(0,i18n_1.__)("Drop here to remove","woocommerce")})):(0,element_1.createElement)(block_slot_fill_1.SectionActions,null,(0,element_1.createElement)("div",{className:"woocommerce-product-form__media-uploader"},(0,element_1.createElement)(components_2.MediaUploader,{value:Array.isArray(l)?l.map((({id:e})=>e)):null!==(o=null==l?void 0:l.id)&&void 0!==o?o:void 0,multipleSelect:!!a&&"add",onError:()=>null,onFileUploadChange:u("product_images_add_via_file_upload_area"),onMediaGalleryOpen:()=>{(0,tracks_1.recordEvent)("product_images_media_gallery_open")},onSelect:function(e){if((0,tracks_1.recordEvent)("product_images_add_via_media_library"),Array.isArray(e)){const r=e.map(mapUploadImageToImage).filter((e=>null!==e));n(r)}else n(mapUploadImageToImage(e))},onUpload:u("product_images_add_via_drag_and_drop_upload"),label:"",buttonText:(0,i18n_1.__)("Choose an image","woocommerce")})))),g?(0,element_1.createElement)(components_2.ImageGallery,{allowDragging:!1,onDragStart:function(e){var r,o;if(Array.isArray(l)){const{id:t,dataset:a}=e.target;if(t)d(parseInt(t,10));else if(null==a?void 0:a.index){const e=parseInt(a.index,10);d(null!==(o=null===(r=l[e])||void 0===r?void 0:r.id)&&void 0!==o?o:null)}c((e=>!e))}},onDragEnd:function(){Array.isArray(l)&&(m&&_&&((0,tracks_1.recordEvent)("product_images_remove_image_button_click"),n(l.filter((e=>e.id!==_))),s(!1),d(null)),c((e=>!e)))},onOrderChange:function(e){if(Array.isArray(l)){const r=l.reduce(((e,r)=>({...e,[`${r.id}`]:r})),{}),o=e.filter((e=>{var o;return(null===(o=null==e?void 0:e.props)||void 0===o?void 0:o.id)in r})).map((e=>{var o;return r[null===(o=null==e?void 0:e.props)||void 0===o?void 0:o.id]}));(0,tracks_1.recordEvent)("product_images_change_image_order_via_image_gallery"),n(o)}},onReplace:function({replaceIndex:e,media:r}){if((0,tracks_1.recordEvent)("product_images_replace_image_button_click"),Array.isArray(l)&&!l.some((e=>r.id===e.id))){const o=mapUploadImageToImage(r);if(o){const r=[...l];r[e]=o,n(r)}}else n(mapUploadImageToImage(r))},onRemove:function({removedItem:e}){if((0,tracks_1.recordEvent)("product_images_remove_image_button_click"),Array.isArray(l)){const r=l.filter((r=>r.id===e.props.id));n(r)}else n(null)},onSelectAsCover:()=>(0,tracks_1.recordEvent)("product_images_select_image_as_cover_button_click")},(Array.isArray(l)?l:[l]).map(((e,r)=>(0,element_1.createElement)(components_2.ImageGalleryItem,{key:e.id,alt:e.alt,src:e.src,id:`${e.id}`,isCover:a&&0===r})))):(0,element_1.createElement)(place_holder_1.PlaceHolder,null))}exports.ImageBlockEdit=ImageBlockEdit;