"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var framepay_error_1 = __importDefault(require("../../framepay-error"));
var base_element_1 = __importDefault(require("./base-element"));
var CardElement = /** @class */ (function (_super) {
    __extends(CardElement, _super);
    function CardElement() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CardElement.prototype.setupElement = function () {
        var _this = this;
        var _a = this.props, onReady = _a.onReady, onChange = _a.onChange, onFocus = _a.onFocus, onBlur = _a.onBlur, elementType = _a.elementType;
        var makeElement = function () {
            // elementNode already checked in BaseElement.handleSetupElement
            // just ts checks fix
            if (!_this.elementNode) {
                throw (0, framepay_error_1.default)({
                    code: framepay_error_1.default.codes.elementMountError,
                    details: "CardElement invalid elementNode, elementType: ".concat(elementType || 'default'),
                });
            }
            try {
                return _this.props.Framepay.card.mount(_this.elementNode, elementType);
            }
            catch (e) {
                throw (0, framepay_error_1.default)({
                    code: framepay_error_1.default.codes.elementMountError,
                    details: "CardElement error in remote api call, elementType: ".concat(elementType || 'default'),
                    trace: e,
                });
            }
        };
        var element = makeElement();
        try {
            element.on('ready', function () {
                _this.setState({ ready: true }, function () {
                    if (onReady) {
                        onReady();
                    }
                });
            });
            element.on('change', function (data) {
                if (onChange) {
                    onChange(data);
                }
            });
            element.on('focus', function () {
                if (onFocus) {
                    onFocus();
                }
            });
            element.on('blur', function () {
                if (onBlur) {
                    onBlur();
                }
            });
            this.setState({ element: element });
        }
        catch (e) {
            throw (0, framepay_error_1.default)({
                code: framepay_error_1.default.codes.elementMountError,
                details: "CardElement events binding error, elementType: ".concat(elementType || 'default'),
                trace: e,
            });
        }
    };
    return CardElement;
}(base_element_1.default));
exports.default = CardElement;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2FyZC1lbGVtZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2xpYi9jb21wb25lbnRzL2VsZW1lbnRzL2NhcmQtZWxlbWVudC50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFDQSx3RUFBaUQ7QUFDakQsZ0VBQXlDO0FBSXpDO0lBQXlDLCtCQUFpQztJQUExRTs7SUF1RUEsQ0FBQztJQXRFRyxrQ0FBWSxHQUFaO1FBQUEsaUJBcUVDO1FBcEVTLElBQUEsS0FBc0QsSUFBSSxDQUFDLEtBQUssRUFBOUQsT0FBTyxhQUFBLEVBQUUsUUFBUSxjQUFBLEVBQUUsT0FBTyxhQUFBLEVBQUUsTUFBTSxZQUFBLEVBQUUsV0FBVyxpQkFBZSxDQUFDO1FBRXZFLElBQU0sV0FBVyxHQUFHO1lBQ2hCLGdFQUFnRTtZQUNoRSxxQkFBcUI7WUFDckIsSUFBSSxDQUFDLEtBQUksQ0FBQyxXQUFXLEVBQUUsQ0FBQztnQkFDcEIsTUFBTSxJQUFBLHdCQUFhLEVBQUM7b0JBQ2hCLElBQUksRUFBRSx3QkFBYSxDQUFDLEtBQUssQ0FBQyxpQkFBaUI7b0JBQzNDLE9BQU8sRUFBRSx3REFDTCxXQUFXLElBQUksU0FBUyxDQUMxQjtpQkFDTCxDQUFDLENBQUM7WUFDUCxDQUFDO1lBRUQsSUFBSSxDQUFDO2dCQUNELE9BQU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