import { __decorate, __metadata } from "tslib";
import '@material/web/button/elevated-button.js';
import './role-selector.js';
import gql from 'graphql-tag';
import { css, html, LitElement } from 'lit';
import { customElement, property, query } from 'lit/decorators.js';
import { client, gqlContext } from '@operato/graphql';
import { i18next, localize } from '@operato/i18n';
import { OxPrompt } from '@operato/popup/ox-prompt.js';
import { ButtonContainerStyles } from '@operato/styles';
import { RoleSelector } from './role-selector';
let PartnerRoleEditor = class PartnerRoleEditor extends localize(i18next)(LitElement) {
    constructor() {
        super(...arguments);
        this.roles = [];
        this.grantingRoles = [];
    }
    render() {
        const roles = this.roles || [];
        const grantingRoles = this.grantingRoles || [];
        return html `
      <role-selector .roles="${roles}" .userRoles="${grantingRoles}"></role-selector>

      <div class="button-container">
        <md-elevated-button @click=${this.onSave}>${i18next.t('button.save')}</md-elevated-button>
        <md-elevated-button @click=${this.onTerminateContract} danger
          >${i18next.t('button.terminate contract')}</md-elevated-button
        >
      </div>
    `;
    }
    updated(changes) {
        if (changes.has('customer')) {
            this.refreshRoles();
        }
    }
    async refreshRoles() {
        var _a;
        const response = await client.query({
            query: gql `
        query roles($customerId: String!) {
          roles {
            items {
              id
              name
              description
            }
            total
          }
          grantingRoles(customerId: $customerId) {
            id
            role {
              id
              name
              description
            }
          }
        }
      `,
            variables: { customerId: this.customer.id },
            context: gqlContext()
        });
        if (!((_a = response.errors) === null || _a === void 0 ? void 0 : _a.length)) {
            this.roles = response.data.roles.items;
            this.grantingRoles = response.data.grantingRoles.map(grantingRole => grantingRole.role);
        }
        else {
            this.roles = [];
            this.grantingRoles = [];
        }
    }
    async onSave() {
        var _a;
        const roles = this.roleSelector.selectedRoles();
        const response = await client.mutate({
            mutation: gql `
        mutation grantRoles($customerId: String!, $roles: [RolePatch!]!) {
          grantRoles(customerId: $customerId, roles: $roles)
        }
      `,
            variables: { customerId: this.customer.id, roles },
            context: gqlContext()
        });
        if (!((_a = response.errors) === null || _a === void 0 ? void 0 : _a.length)) {
            this.showToast(i18next.t('text.data_updated_successfully'));
        }
    }
    async onTerminateContract() {
        var _a;
        if (await OxPrompt.open({
            title: i18next.t('text.are_you_sure'),
            text: i18next.t('text.are_you_sure_to_terminate_contract'),
            confirmButton: { text: i18next.t('button.terminate contract') },
            cancelButton: { text: i18next.t('button.cancel') }
        })) {
            const response = await client.mutate({
                mutation: gql `
          mutation terminateContract($partnerName: String!) {
            terminateContract(partnerName: $partnerName)
          }
        `,
                variables: { partnerName: this.customer.name },
                context: gqlContext()
            });
            if (!((_a = response.errors) === null || _a === void 0 ? void 0 : _a.length)) {
                const answer = this.dispatchEvent(new CustomEvent('contractTerminated'));
                if (answer) {
                    await OxPrompt.open({
                        type: 'success',
                        title: i18next.t('text.completed'),
                        confirmButton: { text: i18next.t('button.confirm') }
                    });
                }
            }
        }
    }
    showToast(message) {
        document.dispatchEvent(new CustomEvent('notify', { detail: { message, option: { timer: 1000 } } }));
    }
};
PartnerRoleEditor.styles = [
    ButtonContainerStyles,
    css `
      :host {
        display: flex;
        flex-direction: column;

        border: 1px solid var(--md-sys-color-primary);
        padding: var(--spacing-medium);
        font: normal 15px var(--theme-font);
        color: var(--md-sys-color-secondary);
      }

      md-elevated-button {
        margin-right: var(--spacing-small);
      }
    `
];
__decorate([
    property({ type: Object }),
    __metadata("design:type", Object)
], PartnerRoleEditor.prototype, "customer", void 0);
__decorate([
    property({ type: Array }),
    __metadata("design:type", Array)
], PartnerRoleEditor.prototype, "roles", void 0);
__decorate([
    property({ type: Array }),
    __metadata("design:type", Array)
], PartnerRoleEditor.prototype, "grantingRoles", void 0);
__decorate([
    query('role-selector'),
    __metadata("design:type", RoleSelector)
], PartnerRoleEditor.prototype, "roleSelector", void 0);
PartnerRoleEditor = __decorate([
    customElement('partner-role-editor')
], PartnerRoleEditor);
//# sourceMappingURL=partner-role-editor.js.map