"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Edit=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");function mapUploadImageToImage(e){return e.id?{id:e.id,name:e.title,src:e.url,alt:e.alt}:null}function Edit({attributes:e,context:r}){var o;const{property:a,multiple:t}=e,[n,l]=(0,core_data_1.useEntityProp)("postType",r.postType,a),[i,c]=(0,element_1.useState)(!1),[s,m]=(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(n)?n.length>0:Boolean(n)})});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)&&l([...n,...e])}else r.id&&l(mapUploadImageToImage(r))}}const v=null!==n&&(!Array.isArray(n)||n.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:()=>m(!0),onDrop:()=>m(!0),label:(0,i18n_1.__)("Drop here to remove","woocommerce")})):(0,element_1.createElement)(components_2.MediaUploader,{value:Array.isArray(n)?n.map((({id:e})=>e)):null!==(o=null==n?void 0:n.id)&&void 0!==o?o:void 0,multipleSelect:!!t&&"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));l(r)}else l(mapUploadImageToImage(e))},onUpload:u("product_images_add_via_drag_and_drop_upload"),label:"",buttonText:(0,i18n_1.__)("Choose an image","woocommerce")})),v?(0,element_1.createElement)(components_2.ImageGallery,{allowDragging:!1,onDragStart:function(e){var r,o;if(Array.isArray(n)){const{id:a,dataset:t}=e.target;if(a)d(parseInt(a,10));else if(null==t?void 0:t.index){const e=parseInt(t.index,10);d(null!==(o=null===(r=n[e])||void 0===r?void 0:r.id)&&void 0!==o?o:null)}c((e=>!e))}},onDragEnd:function(){Array.isArray(n)&&(s&&_&&((0,tracks_1.recordEvent)("product_images_remove_image_button_click"),l(n.filter((e=>e.id!==_))),m(!1),d(null)),c((e=>!e)))},onOrderChange:function(e){if(Array.isArray(n)){const r=n.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"),l(o)}},onReplace:function({replaceIndex:e,media:r}){if((0,tracks_1.recordEvent)("product_images_replace_image_button_click"),Array.isArray(n)&&!n.some((e=>r.id===e.id))){const o=mapUploadImageToImage(r);if(o){const r=[...n];r[e]=o,l(r)}}else l(mapUploadImageToImage(r))},onRemove:function({removedItem:e}){if((0,tracks_1.recordEvent)("product_images_remove_image_button_click"),Array.isArray(n)){const r=n.filter((r=>r.id===e.props.id));l(r)}else l(null)},onSelectAsCover:()=>(0,tracks_1.recordEvent)("product_images_select_image_as_cover_button_click")},(Array.isArray(n)?n:[n]).map(((e,r)=>(0,element_1.createElement)(components_2.ImageGalleryItem,{key:e.id,alt:e.alt,src:e.src,id:`${e.id}`,isCover:t&&0===r})))):(0,element_1.createElement)(place_holder_1.PlaceHolder,null))}exports.Edit=Edit;