"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 components_1=require("@woocommerce/components"),tracks_1=require("@woocommerce/tracks"),block_editor_1=require("@wordpress/block-editor"),components_2=require("@wordpress/components"),core_data_1=require("@wordpress/core-data"),element_1=require("@wordpress/element"),i18n_1=require("@wordpress/i18n"),moment_1=__importDefault(require("moment")),date_1=require("@wordpress/date"),validation_context_1=require("../../contexts/validation-context");function Edit({clientId:e}){const t=(0,block_editor_1.useBlockProps)(),o=(0,date_1.getSettings)().formats.datetime,[r,a]=(0,element_1.useState)(!1),[n]=(0,core_data_1.useEntityProp)("postType","product","sale_price"),_=Number.parseFloat(n||"0")>0,[l,c]=(0,core_data_1.useEntityProp)("postType","product","date_on_sale_from_gmt"),[i,m]=(0,core_data_1.useEntityProp)("postType","product","date_on_sale_to_gmt"),s=(0,moment_1.default)().startOf("minute").toISOString();(0,element_1.useEffect)((()=>{_||(a(!1),c(""),m(""))}),[_]),(0,element_1.useEffect)((()=>{(l||i)&&a(!0)}),[l,i]);const d=(0,moment_1.default)(l,moment_1.default.ISO_8601,!0),u=(0,moment_1.default)(i,moment_1.default.ISO_8601,!0),{ref:p,error:f,validate:h}=(0,validation_context_1.useValidation)(`date_on_sale_from_gmt-${e}`,(async function(){if(r&&l){if(!d.isValid())return(0,i18n_1.__)("Please enter a valid date.","woocommerce");if(d.isAfter(u))return(0,i18n_1.__)("The start date of the sale must be before the end date.","woocommerce")}}),[r,l,d,u]),{ref:w,error:E,validate:b}=(0,validation_context_1.useValidation)(`date_on_sale_to_gmt-${e}`,(async function(){if(r&&i){if(!u.isValid())return(0,i18n_1.__)("Please enter a valid date.","woocommerce");if(u.isBefore(d))return(0,i18n_1.__)("The end date of the sale must be after the start date.","woocommerce")}}),[r,l,d,u]);return(0,element_1.createElement)("div",{...t},(0,element_1.createElement)(components_2.ToggleControl,{label:(0,i18n_1.__)("Schedule sale","woocommerce"),checked:r,onChange:function(e){(0,tracks_1.recordEvent)("product_pricing_schedule_sale_toggle_click",{enabled:e}),a(e),e?(c(s),m("")):(c(""),m(""))},disabled:!_}),r&&(0,element_1.createElement)("div",{className:"wp-block-columns"},(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(components_1.DateTimePickerControl,{ref:p,label:(0,i18n_1.__)("From","woocommerce"),placeholder:(0,i18n_1.__)("Sale start date and time (optional)","woocommerce"),dateTimeFormat:o,currentDate:l,onChange:c,className:f&&"has-error",help:f,onBlur:h})),(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(components_1.DateTimePickerControl,{ref:w,label:(0,i18n_1.__)("To","woocommerce"),placeholder:(0,i18n_1.__)("Sale end date and time (optional)","woocommerce"),dateTimeFormat:o,currentDate:i,onChange:e=>m((0,moment_1.default)(e).startOf("minute").toISOString()),onBlur:b,className:E&&"has-error",help:E}))))}exports.Edit=Edit;