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