import * as i0 from '@angular/core';
import { Input, Optional, Inject, Component, NgModule } from '@angular/core';
import * as i1 from '@angular/platform-browser';
import { BASE_URL } from '@aztrix/environment';
import { forwardLink$, isOfType, isProfile, isMeProfile, displayName, isGroup, isProposal } from '@aztrix/helpers';
import { ForwardType } from '@aztrix/models';
import * as i2 from '@aztrix/translate';
import { first } from 'rxjs/operators';
import * as i3 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i4 from '@aztrix/icons';
import { IconsModule } from '@aztrix/icons';
import * as i5 from '@aztrix/components/button';
import { ButtonModule } from '@aztrix/components/button';
import { RouterModule } from '@angular/router';

class ForwardButtonComponent {
    constructor(_sanitizer, _translate, _baseUrl) {
        this._sanitizer = _sanitizer;
        this._translate = _translate;
        this._baseUrl = _baseUrl;
        this._subjectLink = '';
    }
    ngOnChanges() {
        if (this.url) {
            this._subjectLink = this.url;
        }
        else {
            forwardLink$(this.subject, this._baseUrl)
                .pipe(first())
                .subscribe((forwardLink) => {
                this._subjectLink = forwardLink;
            });
        }
    }
    forwardIcon(type) {
        switch (type) {
            case ForwardType.QR:
                return 'qrcode';
            case ForwardType.EMAIL:
                return 'email-outline';
            case ForwardType.LINKEDIN:
            case ForwardType.WHATSAPP:
            case ForwardType.FACEBOOK:
            case ForwardType.TWITTER:
                return `brands:${type.toLowerCase()}`;
            case ForwardType.LINK:
                return type.toLowerCase();
            case ForwardType.EMBED:
                return 'code-tags';
            default:
                return undefined;
        }
    }
    shareLink(type) {
        switch (type) {
            case ForwardType.TWITTER:
                // eslint-disable-next-line max-len
                return `https://twitter.com/intent/tweet?text=${this._shareText} ${this._subjectLink}`.replace(/ /g, '%20');
            case ForwardType.WHATSAPP:
                return this._sanitizer.bypassSecurityTrustUrl(`whatsapp://send?text=${this._shareText} ${this._subjectLink}`);
            case ForwardType.FACEBOOK:
                return `https://www.facebook.com/sharer/sharer.php?u=${this._subjectLink?.replace(':', '%3A')}`;
            case ForwardType.LINKEDIN:
                return `https://www.linkedin.com/shareArticle?mini=true&url=${this._subjectLink}&title=${this._shareText.replace(/ /g, '%20')}`;
            case ForwardType.EMAIL:
                return `mailto:?body=${this._translate.instant('forward.email.body')} ${this._subjectLink}&subject=${this._shareText}`;
            default:
                return this._sanitizer.bypassSecurityTrustUrl('javascript:void(0)');
        }
    }
    linkTarget(type) {
        return isOfType({ type: type }, ForwardType.VCARD, ForwardType.LINK, ForwardType.QR, ForwardType.WHATSAPP, ForwardType.EMBED)
            ? '_self'
            : '_blank';
    }
    socialColor(type) {
        return isOfType({ type: type }, ForwardType.LINKEDIN, ForwardType.WHATSAPP, ForwardType.FACEBOOK, ForwardType.TWITTER, ForwardType.INSTAGRAM)
            ? type.toLowerCase()
            : 'primary';
    }
    get _shareText() {
        if (isProfile(this.subject) && isMeProfile(this.subject)) {
            return this._translate.instant('forward.link.text.profile');
        }
        if (isProfile(this.subject)) {
            return this._translate.instant('forward.link.text.contact', {
                contact: displayName(this._translate, this.subject),
            });
        }
        if (isGroup(this.subject)) {
            return this._translate.instant('forward.link.text.group', {
                group: displayName(this._translate, this.subject),
            });
        }
        if (isProposal(this.subject)) {
            return this._translate.instant('forward.link.text.proposal');
        }
        return this._translate.instant('forward.link.text.page');
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ForwardButtonComponent, deps: [{ token: i1.DomSanitizer }, { token: i2.TranslateService }, { token: BASE_URL, optional: true }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: ForwardButtonComponent, selector: "ax-forward-button", inputs: { subject: "subject", forwardType: "forwardType", url: "url" }, usesOnChanges: true, ngImport: i0, template: "<a\n  axButton\n  icon-button\n  filled\n  class=\"forward-button\"\n  [color]=\"socialColor(forwardType)\"\n  [href]=\"shareLink(forwardType)\"\n  [target]=\"linkTarget(forwardType)\"\n>\n  <ax-icon\n    class=\"icon\"\n    *ngIf=\"forwardIcon(forwardType); else vCard\"\n    [name]=\"forwardIcon(forwardType)\"\n  ></ax-icon>\n</a>\n\n<ng-template #vCard>\n  <span class=\"text-icon\">vCard</span>\n</ng-template>\n", styles: [":host{width:inherit;height:inherit}:host ::ng-deep .ax-modal-body-content{background-color:#efefef}:host ::ng-deep .forward-button.ax-button.button-filled{width:100%;height:100%}:host ::ng-deep .forward-button.ax-button.button-filled .icon{width:50%;height:50%}.forward-button.ax-button{border-radius:30px}.text-icon{font-size:.625rem;text-transform:none}\n"], dependencies: [{ kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i4.IconComponent, selector: "ax-icon", inputs: ["name", "inline"] }, { kind: "directive", type: i5.ButtonDirective, selector: "[axButton]", inputs: ["color", "labelHideMobile", "smallButton", "bigButton", "fullWidth", "filled", "outline", "icon-button", "hoverLabel", "hover", "lowercase", "disabled"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ForwardButtonComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-forward-button', template: "<a\n  axButton\n  icon-button\n  filled\n  class=\"forward-button\"\n  [color]=\"socialColor(forwardType)\"\n  [href]=\"shareLink(forwardType)\"\n  [target]=\"linkTarget(forwardType)\"\n>\n  <ax-icon\n    class=\"icon\"\n    *ngIf=\"forwardIcon(forwardType); else vCard\"\n    [name]=\"forwardIcon(forwardType)\"\n  ></ax-icon>\n</a>\n\n<ng-template #vCard>\n  <span class=\"text-icon\">vCard</span>\n</ng-template>\n", styles: [":host{width:inherit;height:inherit}:host ::ng-deep .ax-modal-body-content{background-color:#efefef}:host ::ng-deep .forward-button.ax-button.button-filled{width:100%;height:100%}:host ::ng-deep .forward-button.ax-button.button-filled .icon{width:50%;height:50%}.forward-button.ax-button{border-radius:30px}.text-icon{font-size:.625rem;text-transform:none}\n"] }]
        }], ctorParameters: () => [{ type: i1.DomSanitizer }, { type: i2.TranslateService }, { type: undefined, decorators: [{
                    type: Optional
                }, {
                    type: Inject,
                    args: [BASE_URL]
                }] }], propDecorators: { subject: [{
                type: Input
            }], forwardType: [{
                type: Input
            }], url: [{
                type: Input
            }] } });

class ForwardButtonModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ForwardButtonModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: ForwardButtonModule, declarations: [ForwardButtonComponent], imports: [CommonModule, IconsModule, RouterModule, ButtonModule], exports: [ForwardButtonComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ForwardButtonModule, imports: [CommonModule, IconsModule, RouterModule, ButtonModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ForwardButtonModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [ForwardButtonComponent],
                    imports: [CommonModule, IconsModule, RouterModule, ButtonModule],
                    exports: [ForwardButtonComponent],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { ForwardButtonComponent, ForwardButtonModule };
//# sourceMappingURL=aztrix-components-forward-button.mjs.map
