import * as React from 'react';
import FramePayError from '../../framepay-error';
import BaseElement from './base-element';
import type {
    SamsungPayProps,
    SamsungPayState,
} from '../../../../types/elements';

export default class SamsungPayElement extends BaseElement<
    SamsungPayProps,
    SamsungPayState
> {
    setupElement() {
        const { Framepay } = this.props;

        const makeElement = () => {
            // elementNode already checked in BaseElement.handleSetupElement
            // just ts checks fix
            if (!this.elementNode) {
                throw FramePayError({
                    code: FramePayError.codes.elementMountError,
                    details: `SamsungPayElement invalid elementNode`,
                });
            }

            try {
                return Framepay.samsungPay.mount(this.elementNode);
            } catch (e) {
                throw FramePayError({
                    code: FramePayError.codes.elementMountError,
                    details: `SamsungPayElement error in remote api call`,
                    trace: e,
                });
            }
        };

        const element = makeElement();
        this.setState({ element });
    }
}
