"use strict";var __create=Object.create,__defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__getProtoOf=Object.getPrototypeOf,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,t)=>{for(var o in t)__defProp(e,o,{get:t[o],enumerable:!0})},__copyProps=(e,t,o,r)=>{if(t&&"object"==typeof t||"function"==typeof t)for(let m of __getOwnPropNames(t))__hasOwnProp.call(e,m)||m===o||__defProp(e,m,{get:()=>t[m],enumerable:!(r=__getOwnPropDesc(t,m))||r.enumerable});return e},__toESM=(e,t,o)=>(o=null!=e?__create(__getProtoOf(e)):{},__copyProps(!t&&e&&e.__esModule?o:__defProp(o,"default",{value:e,enumerable:!0}),e)),__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),edit_exports={};__export(edit_exports,{Edit:()=>Edit}),module.exports=__toCommonJS(edit_exports);var import_block_templates=require("@woocommerce/block-templates"),import_components=require("@woocommerce/components"),import_tracks=require("@woocommerce/tracks"),import_components2=require("@wordpress/components"),import_core_data=require("@wordpress/core-data"),import_element=require("@wordpress/element"),import_i18n=require("@wordpress/i18n"),import_moment=__toESM(require("moment")),import_date=require("@wordpress/date"),import_use_product_edits=require("../../../hooks/use-product-edits"),import_validation_context=require("../../../contexts/validation-context");function Edit({attributes:e,clientId:t,context:o}){const r=(0,import_block_templates.useWooBlockProps)(e),{hasEdit:m}=(0,import_use_product_edits.useProductEdits)(),_=(0,import_date.getSettings)().formats.datetime,[a,i]=(0,import_element.useState)(!1),[n]=(0,import_core_data.useEntityProp)("postType",o.postType||"product","sale_price"),p=Number.parseFloat(n||"0")>0,[c,s]=(0,import_core_data.useEntityProp)("postType",o.postType||"product","date_on_sale_from_gmt"),[l,d]=(0,import_core_data.useEntityProp)("postType",o.postType||"product","date_on_sale_to_gmt"),u=(0,import_moment.default)().startOf("minute").toISOString();(0,import_element.useEffect)(()=>{m("sale_price")&&!p&&(i(!1),s(""),d(""))},[p]),(0,import_element.useEffect)(()=>{(c||l)&&i(!0)},[c,l]);const f=(0,import_moment.default)(c,import_moment.default.ISO_8601,!0),g=(0,import_moment.default)(l,import_moment.default.ISO_8601,!0),{ref:w,error:P,validate:b}=(0,import_validation_context.useValidation)(`date_on_sale_from_gmt-${t}`,async function(){if(a&&c){if(!f.isValid())return{message:(0,import_i18n.__)("Please enter a valid date.","woocommerce")};if(f.isAfter(g))return{message:(0,import_i18n.__)("The start date of the sale must be before the end date.","woocommerce")}}},[a,c,f,g]),{ref:h,error:O,validate:y}=(0,import_validation_context.useValidation)(`date_on_sale_to_gmt-${t}`,async function(){if(a&&l){if(!g.isValid())return{message:(0,import_i18n.__)("Please enter a valid date.","woocommerce")};if(g.isBefore(f))return{message:(0,import_i18n.__)("The end date of the sale must be after the start date.","woocommerce")}}},[a,c,f,g]);return(0,import_element.createElement)("div",{...r},(0,import_element.createElement)(import_components2.ToggleControl,{label:(0,import_i18n.__)("Schedule sale","woocommerce"),checked:a,onChange:function(e){(0,import_tracks.recordEvent)("product_pricing_schedule_sale_toggle_click",{enabled:e}),i(e),e?(s(u),d("")):(s(""),d(""))},disabled:!p}),a&&(0,import_element.createElement)("div",{className:"wp-block-columns wp-block-woocommerce-product-schedule-sale-fields__content"},(0,import_element.createElement)("div",{className:"wp-block-column"},(0,import_element.createElement)(import_components.DateTimePickerControl,{ref:w,label:(0,import_i18n.__)("From","woocommerce"),placeholder:(0,import_i18n.__)("Sale start date and time (optional)","woocommerce"),dateTimeFormat:_,currentDate:c,onChange:s,className:P&&"has-error",help:P,onBlur:()=>b()})),(0,import_element.createElement)("div",{className:"wp-block-column"},(0,import_element.createElement)(import_components.DateTimePickerControl,{ref:h,label:(0,import_i18n.__)("To","woocommerce"),placeholder:(0,import_i18n.__)("Sale end date and time (optional)","woocommerce"),dateTimeFormat:_,currentDate:l,onChange:e=>d((0,import_moment.default)(e).startOf("minute").toISOString()),onBlur:()=>y(),className:O&&"has-error",help:O}))))}