import { copyToClipboard } from './utils/copy-to-clipboard';
import { isApp } from './utils/helpers';

var Wick = window.Wick || {};

Wick.MyAccount = {
    el: {
        $clipboardCode: $('.clipboard-code'),
        appInfo: '.app-info',
        hideAppInfo: 'app-info--hide',
    },
    copyCode($btn) {
        const $container = $btn.closest('.clipboard-code');
        const value = $container.find('.subtitle').text().trim();
        const title = $container.find('.title').text().trim();

        copyToClipboard(value, $btn);
        Wick.SnackBar.show({
            text: `${title} copied to clipboard`,
            type: Wick.SnackBar.types.SUCCESS,
            withCloseButton: false,
            autoClose: true,
            delayBeforeClose: 1500,
        });
    },
    init() {
        Wick.MyAccount.el.$clipboardCode.find('.copy-code').on('click', function () {
            Wick.MyAccount.copyCode($(this));
        });

        if ($(Wick.MyAccount.el.appInfo).length && isApp()) {
            $(Wick.MyAccount.el.appInfo).addClass(Wick.MyAccount.el.hideAppInfo);
        }
    },
};

$(document).ready(function () {
    if (!Wick.MyAccount.el.$clipboardCode.length) {
        return;
    }

    Wick.MyAccount.init();
});
