/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
import{html as m}from"lit";import{property as k,state as p,customElement as f}from"lit/decorators.js";import s from"~labels/core.bookmarks";import{PseudoButtonLitElement as y}from"@typo3/backend/element/pseudo-button.js";import n,{BookmarkStoreChangedEvent as u}from"@typo3/backend/bookmark/bookmark-store.js";import b from"@typo3/backend/notification.js";import"@typo3/backend/element/icon-element.js";import"@typo3/backend/element/spinner-element.js";var r=function(c,e,o,a){var d=arguments.length,i=d<3?e:a===null?a=Object.getOwnPropertyDescriptor(e,o):a,l;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(c,e,o,a);else for(var h=c.length-1;h>=0;h--)(l=c[h])&&(i=(d<3?l(i):d>3?l(e,o,i):l(e,o))||i);return d>3&&i&&Object.defineProperty(e,o,i),i};let t=class extends y{constructor(){super(...arguments),this.route="",this.arguments="",this.displayName="",this.hideLabelText=!1,this.isBookmarked=!1,this.isProcessing=!1,this.handleStoreUpdate=async()=>{await this.checkIfBookmarked(),this.updateState()}}get bookmarkKey(){return n.getBookmarkKey(this.route,this.arguments)}connectedCallback(){super.connectedCallback(),document.addEventListener(u,this.handleStoreUpdate),this.setup()}disconnectedCallback(){super.disconnectedCallback(),document.removeEventListener(u,this.handleStoreUpdate)}createRenderRoot(){return this}render(){const e=this.isBookmarked?s.get("action.remove"):s.get("action.create"),o=this.hideLabelText?m`<span class=visually-hidden>${e}</span>`:m`${e}`;if(this.isProcessing)return m`<typo3-backend-spinner size=small></typo3-backend-spinner>${o}`;const a=this.isBookmarked?"actions-bookmark-remove":"actions-bookmark-add";return m`<typo3-backend-icon identifier=${a} size=small></typo3-backend-icon>${o}`}async buttonActivated(){if(!this.isProcessing){this.isProcessing=!0,this.updateState();try{if(this.isBookmarked){const e=await n.getBookmark(this.bookmarkKey);e&&n.requestDelete(e.id)}else{const e=await n.create(this.route,this.arguments,this.displayName);e.success?b.success(s.get("success.created.title"),s.get("success.created.message")):b.error(s.get("error.createFailed.title"),e.error||s.get("error.createFailed.message"))}}finally{this.isProcessing=!1,await this.checkIfBookmarked(),this.updateState()}}}async setup(){await this.checkIfBookmarked(),this.updateState()}async checkIfBookmarked(){this.isBookmarked=await n.isBookmarked(this.bookmarkKey)}updateState(){this.isProcessing?(this.setAttribute("aria-disabled","true"),this.tabIndex=-1):(this.removeAttribute("aria-disabled"),this.tabIndex=0),this.setAttribute("aria-pressed",this.isBookmarked?"true":"false"),this.title=this.isBookmarked?s.get("action.remove"):s.get("action.create")}};r([k({type:String})],t.prototype,"route",void 0),r([k({type:String})],t.prototype,"arguments",void 0),r([k({type:String,attribute:"display-name"})],t.prototype,"displayName",void 0),r([k({type:Boolean,attribute:"hide-label-text"})],t.prototype,"hideLabelText",void 0),r([p()],t.prototype,"isBookmarked",void 0),r([p()],t.prototype,"isProcessing",void 0),t=r([f("typo3-backend-bookmark-button")],t);export{t as BookmarkButtonElement};
