"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_validation_1=require("../../hooks/use-validation");function Edit({}){const e=(0,block_editor_1.useBlockProps)({className:"wp-block-woocommerce-product-schedule-sale-fields"}),t=(0,date_1.getSettings)().formats.datetime,[o,r]=(0,element_1.useState)(!1),[a]=(0,core_data_1.useEntityProp)("postType","product","sale_price"),n=Number.parseFloat(a||"0")>0,[c,_]=(0,core_data_1.useEntityProp)("postType","product","date_on_sale_from_gmt"),[m,l]=(0,core_data_1.useEntityProp)("postType","product","date_on_sale_to_gmt"),s=(0,moment_1.default)().startOf("minute").toISOString();(0,element_1.useEffect)((()=>{n||(r(!1),_(""),l(""))}),[n]),(0,element_1.useEffect)((()=>{(c||m)&&r(!0)}),[c,m]);const i=(0,use_validation_1.useValidation)("product/date_on_sale_to_gmt",(()=>!o||!m||(0,moment_1.default)(c).isBefore(m)));return(0,element_1.createElement)("div",{...e},(0,element_1.createElement)(components_2.ToggleControl,{label:(0,i18n_1.__)("Schedule sale","woocommerce"),checked:o,onChange:function(e){(0,tracks_1.recordEvent)("product_pricing_schedule_sale_toggle_click",{enabled:e}),r(e),e?(_(s),l("")):(_(""),l(""))},disabled:!n}),o&&(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,{label:(0,i18n_1.__)("From","woocommerce"),placeholder:(0,i18n_1.__)("Sale start date and time (optional)","woocommerce"),dateTimeFormat:t,currentDate:c,onChange:_})),(0,element_1.createElement)("div",{className:"wp-block-column"},(0,element_1.createElement)(components_1.DateTimePickerControl,{label:(0,i18n_1.__)("To","woocommerce"),placeholder:(0,i18n_1.__)("Sale end date and time (optional)","woocommerce"),dateTimeFormat:t,currentDate:m,onChange:e=>l((0,moment_1.default)(e).startOf("minute").toISOString()),className:i?void 0:"has-error",help:i?void 0:(0,i18n_1.__)("To date must be greater than From date.","woocommerce")}))))}exports.Edit=Edit;