Liferay.Loader.define("commerce-frontend-taglib@19.1.14/add_organizations_modal/AddOrganizationsModal.es",["module","exports","require","frontend-js-web","frontend-js-metal-web$metal-component","frontend-js-metal-web$metal-soy","./AddOrganizationsModal.soy","frontend-taglib-clay$clay-modal","./OrganizationInputItem.es","./OrganizationListItem.es","../css/main.scss"],(function(e,t,n){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=t.AddOrganizationModal=void 0;var i=n("frontend-js-web"),r=_interopRequireDefault(n("frontend-js-metal-web$metal-component")),o=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 n={},i=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var r in e)if(Object.prototype.hasOwnProperty.call(e,r)){var o=i?Object.getOwnPropertyDescriptor(e,r):null;o&&(o.get||o.set)?Object.defineProperty(n,r,o):n[r]=e[r]}n.default=e,t&&t.set(e,n);return n}(n("frontend-js-metal-web$metal-soy")),a=_interopRequireDefault(n("./AddOrganizationsModal.soy"));function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var e=new WeakMap;return _getRequireWildcardCache=function _getRequireWildcardCache(){return e},e}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function ownKeys(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);t&&(i=i.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,i)}return n}function _defineProperty(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}n("frontend-taglib-clay$clay-modal"),n("./OrganizationInputItem.es"),n("./OrganizationListItem.es"),n("../css/main.scss");class AddOrganizationModal extends r.default{created(){this._debouncedFetchOrganizations=(0,i.debounce)(this._fetchOrganizations.bind(this),300)}attached(){return this._fetchOrganizations()}syncAddedOrganizations(){const e=this.element.querySelector(".autocomplete-input__content");this.element.querySelector(".autocomplete-input__box").focus(),e.scrollTo&&e.scrollTo(0,e.offsetHeight)}_handleCloseModal(e){e.preventDefault(),this._modalVisible=!1}syncQuery(){return this._loading=!0,this._debouncedFetchOrganizations()}_handleFormSubmit(e){return e.preventDefault(),this.organizations.length&&(this._toggleItem(this.organizations[0]),this.query=""),e}_handleInputBox(e){return 8!==e.keyCode||this.query.length?(this.query=e.target.value,this.query):(this.selectedOrganizations=this.selectedOrganizations.slice(0,-1),!1)}_toggleItem(e){e.id||(this.query="");const t=this.selectedOrganizations.reduce(((t,n)=>t||n.id===e.id),!1);return this.selectedOrganizations=t?this.selectedOrganizations.filter((t=>t.id!==e.id)):[...this.selectedOrganizations,e],this.selectedOrganizations}_fetchOrganizations(){return(0,i.fetch)(this.organizationsAPI+"?groupId="+themeDisplay.getScopeGroupId()+"&q="+this.query).then((e=>e.json())).then((e=>(this._loading=!1,this.organizations=this.addColorToOrganizations(e.organizations),this.organizations)))}addColorToOrganizations(e){return e.map((e=>function _objectSpread(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(n),!0).forEach((function(t){_defineProperty(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):ownKeys(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}({colorId:Math.floor(6*Math.random())+1},e)))}_addOrganizations(){return!!this.selectedOrganizations.length&&this.emit("addOrganization",this.selectedOrganizations)}toggle(){return this._modalVisible=!this._modalVisible,this._modalVisible}open(){return this._modalVisible=!0,this._modalVisible}close(){return this._modalVisible=!1,this._modalVisible}}t.AddOrganizationModal=AddOrganizationModal,o.default.register(AddOrganizationModal,a.default);const e=o.Config.shapeOf({colorId:o.Config.number(),id:o.Config.oneOfType([o.Config.number(),o.Config.string()]).required(),name:o.Config.string().required()});AddOrganizationModal.STATE={_loading:o.Config.bool().internal().value(!1),_modalVisible:o.Config.bool().internal().value(!1),organizations:o.Config.array(e).value([]),organizationsAPI:o.Config.string().value(""),query:o.Config.string().value(""),selectedOrganizations:o.Config.array(e).value([]),spritemap:o.Config.string()};var s=AddOrganizationModal;t.default=s}}));
//# sourceMappingURL=AddOrganizationsModal.es.js.map