"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 __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var BaseElement = /** @class */ (function (_super) {
    __extends(BaseElement, _super);
    function BaseElement() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            element: null,
            mounted: false,
            ready: false,
        };
        _this.elementNode = null;
        return _this;
    }
    BaseElement.prototype.componentWillUnmount = function () {
        if (this.state.mounted && !this.state.element) {
            console.warn("WARNING Element does not exist, please fix the setupElement method and add setState({element})");
            return;
        }
        if (this.state.element) {
            this.state.element.destroy();
        }
    };
    BaseElement.prototype.componentDidMount = function () {
        this.handleSetupElement();
    };
    BaseElement.prototype.setupElement = function () {
        throw new Error("Please implement method setupElement");
    };
    BaseElement.prototype.handleSetupElement = function () {
        if (!this.props.Framepay.ready) {
            /**
             * The remote api isn't ready
             */
            return;
        }
        if (this.state.mounted) {
            /**
             * The field already mounted
             */
            return;
        }
        if (!this.elementNode) {
            /**
             * Component dom element not mounted
             */
            return;
        }
        /**
         * Setup field
         */
        // @ts-expect-error todo fix type issue
        // eslint-disable-next-line react/no-direct-mutation-state
        this.state.mounted = true;
        this.setupElement();
    };
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    BaseElement.prototype.shouldComponentUpdate = function (nextProps, nextState) {
        // we can't use the componentDidUpdate, componentWillReceiveProps methods
        // also, we can't return true here (to avoid the dom element re-render)
        // so, in that case we had to use that method as componentDidUpdate or componentWillReceiveProps
        // with some magic
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        var rules = [
            [this.props.Framepay.ready, nextProps.Framepay.ready],
            [this.state.mounted, nextState.mounted],
            [this.state.ready, nextState.ready],
            [Boolean(this.state.element), Boolean(nextState.element)],
        ];
        var shouldUpdate = rules.find(function (_a) {
            var prev = _a[0], next = _a[1];
            return prev !== next;
        });
        if (shouldUpdate) {
            // @ts-expect-error todo fix typing issues
            this.props = nextProps;
            this.handleSetupElement();
            // @ts-expect-error todo fix typing issues
            // eslint-disable-next-line react/no-direct-mutation-state
            this.state = __assign({}, nextState);
        }
        return false;
    };
    BaseElement.prototype.render = function () {
        var _this = this;
        return (React.createElement("div", { id: this.props.id, ref: function (node) {
                _this.elementNode = node;
            } }));
    };
    return BaseElement;
}(React.Component));
exports.default = BaseElement;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZS1lbGVtZW50LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2xpYi9jb21wb25lbnRzL2VsZW1lbnRzL2Jhc2UtZWxlbWVudC50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLDJDQUErQjtBQU0vQjtJQUdVLCtCQUFxQjtJQUgvQjs7UUFJYSxXQUFLLEdBQUc7WUFDYixPQUFPLEVBQUUsSUFBSTtZQUNiLE9BQU8sRUFBRSxLQUFLO1lBQ2QsS0FBSyxFQUFFLEtBQUs7U0FDVixDQUFDO1FBRUcsaUJBQVcsR0FBMEIsSUFBSSxDQUFDOztJQXdGeEQsQ0FBQztJQXRGRywwQ0FBb0IsR0FBcEI7UUFDSSxJQUFJLElBQUksQ0FBQyxLQUFLLENBQUMsT0FBTyxJQUFJLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxPQUFPLEVBQUUsQ0FBQztZQUM1QyxPQUFPLENBQUMsSUFBSSxDQUNSLGdHQUFnRyxDQUNuRyxDQUFDO1lBQ0YsT0FBTztRQUNYLENBQUM7UUFDRCxJQUFJLElBQUksQ0FBQyxLQUFLLENBQUMsT0FBTyxFQUFFLENBQUM7WUFDckIsSUFBSSxDQUFDLEtBQUssQ0FBQyxPQUFPLENBQUMsT0FBTyxFQUFFLENBQUM7UUFDakMsQ0FBQztJQUNMLENBQUM7SUFFRCx1Q0F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