Liferay.Loader.define("@liferay/commerce-product-definitions-web@7.0.115/js/ProductOptionCheckboxMultiple",["module","exports","require","@frontend-taglib-clay$clayui/form","@liferay/frontend-js-state-web","liferay!frontend-js-react-web$classnames","commerce-frontend-js/utilities/atoms/skuOptionsAtom","liferay!frontend-js-react-web$react","./Asterisk","./utils"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n=_interopRequireWildcard(r("@frontend-taglib-clay$clayui/form")),a=r("@liferay/frontend-js-state-web"),o=_interopRequireDefault(r("liferay!frontend-js-react-web$classnames")),i=_interopRequireDefault(r("commerce-frontend-js/utilities/atoms/skuOptionsAtom")),u=_interopRequireWildcard(r("liferay!frontend-js-react-web$react")),c=_interopRequireDefault(r("./Asterisk")),l=r("./utils");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return _getRequireWildcardCache=function _getRequireWildcardCache(){return e},e}function _interopRequireWildcard(e){if(e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache();if(t&&t.has(e))return t.get(e);var r={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var a in e)if(Object.prototype.hasOwnProperty.call(e,a)){var o=n?Object.getOwnPropertyDescriptor(e,a):null;o&&(o.get||o.set)?Object.defineProperty(r,a,o):r[a]=e[a]}return r.default=e,t&&t.set(e,r),r}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach((function(t){_defineProperty(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var _default=({forceRequired:e,namespace:t,productOption:r})=>{const[s,d]=(0,u.useState)(!1),[f,p]=(0,a.useLiferayState)(i.default),[y,b]=(0,u.useState)(r.productOptionValues);(0,u.useEffect)((()=>p(_objectSpread(_objectSpread({},f),{},{errors:(0,l.getSkuOptionsErrors)(s,r,f),namespace:t}))),[s]),(0,u.useEffect)((()=>{let e=!1,n=f.skuOptions;b(y.map((t=>(t.preselected&&(e=!0,n=[...f.skuOptions,{key:r.key,skuOptionKey:r.key,value:[t.key]}]),_objectSpread(_objectSpread({},t),{},{selected:t.preselected})))));const a=r.required&&!e;return a&&d(!0),p(_objectSpread(_objectSpread({},f),{},{errors:(0,l.getSkuOptionsErrors)(a,r,f),namespace:t,skuOptions:n})),()=>p(l.initialSkuOptionsAtomState)}),[]);const handleChange=({target:{checked:t,value:n}})=>{var a;if(f.updating)return;p(_objectSpread(_objectSpread({},f),{},{updating:!0}));let o=f.skuOptions;const i=o.findIndex((e=>e.skuOptionKey===r.key));o.filter((e=>e.skuOptionKey===r.key))[0]?o[i]={key:r.key,skuOptionKey:r.key,value:t?[...o[i].value,n]:o[i].value.filter((e=>!(e===n)))}:o=[...o,{key:r.key,skuOptionKey:r.key,value:[n]}];const u=y.findIndex((e=>e.key===n));y[u]=_objectSpread(_objectSpread({},y[u]),{},{selected:t}),b(y);const c=(e||r.required)&&0===(null===(a=o[i])||void 0===a?void 0:a.value.length);d(c),p(_objectSpread(_objectSpread({},f),{},{errors:(0,l.getSkuOptionsErrors)(c,r,f),skuOptions:o,updating:!1}))};return u.default.createElement(n.default.Group,{className:(0,o.default)({"has-error":s})},u.default.createElement("label",null,(0,l.getProductOptionName)(r.name),u.default.createElement(c.default,{required:(0,l.isRequired)(e,f.isAdmin,r)})),y.map((({key:e,name:t,selected:r})=>u.default.createElement(n.ClayCheckbox,{checked:r,key:e,label:t,name:e,onChange:handleChange,value:e}))),s&&u.default.createElement(n.default.FeedbackItem,null,u.default.createElement(n.default.FeedbackIndicator,{symbol:"exclamation-full"}),Liferay.Language.get("this-field-is-required")))};t.default=_default}}));
//# sourceMappingURL=ProductOptionCheckboxMultiple.js.map