"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 (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var react_native_video_1 = require("react-native-video");
var builder_block_decorator_1 = require("../decorators/builder-block.decorator");
var react_native_1 = require("react-native");
var DEFAULT_ASPECT_RATIO = 0.7004048582995948;
var Video = /** @class */ (function (_super) {
    __extends(Video, _super);
    function Video() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.video = null;
        return _this;
    }
    Video.prototype.componentDidMount = function () { };
    Video.prototype.render = function () {
        var aspectRatio = this.props.aspectRatio;
        return (react_1.default.createElement(react_native_1.View, { style: { position: 'relative' } },
            react_1.default.createElement(react_native_video_1.default, { paused: !this.props.autoPlay, key: this.props.video || 'no-src', poster: this.props.posterImage, source: { uri: this.props.video }, muted: this.props.muted, controls: this.props.controls, repeat: this.props.loop, resizeMode: this.props.fit, style: __assign({ width: '100%', height: '100%', borderRadius: 1 }, (aspectRatio
                    ? {
                        position: 'absolute',
                    }
                    : null)) }),
            aspectRatio ? (react_1.default.createElement(react_native_1.View, { pointerEvents: "none", style: {
                    width: '100%',
                    paddingTop: aspectRatio * 100 + '%',
                } })) : null));
    };
    Video = __decorate([
        builder_block_decorator_1.BuilderBlock({
            name: 'Video',
            image: 'https://firebasestorage.googleapis.com/v0/b/builder-3b0a2.appspot.com/o/images%2Fbaseline-videocam-24px%20(1).svg?alt=media&token=49a84e4a-b20e-4977-a650-047f986874bb',
            inputs: [
                {
                    name: 'video',
                    type: 'file',
                    allowedFileTypes: ['mp4'],
                    defaultValue: 'https://firebasestorage.googleapis.com/v0/b/builder-3b0a2.appspot.com/o/assets%2FKQlEmWDxA0coC3PK6UvkrjwkIGI2%2F28cb070609f546cdbe5efa20e931aa4b?alt=media&token=912e9551-7a7c-4dfb-86b6-3da1537d1a7f',
                    required: true,
                    onChange: function (options) {
                        var DEFAULT_ASPECT_RATIO = 0.7004048582995948;
                        function loadImage(url, timeout) {
                            if (timeout === void 0) { timeout = 60000; }
                            return new Promise(function (resolve, reject) {
                                var img = document.createElement('img');
                                var loaded = false;
                                img.onload = function () {
                                    loaded = true;
                                    resolve(img);
                                };
                                img.addEventListener('error', function (event) {
                                    console.warn('Image load failed', event.error);
                                    reject(event.error);
                                });
                                img.src = url;
                                setTimeout(function () {
                                    if (!loaded) {
                                        reject(new Error('Image load timed out'));
                                    }
                                }, timeout);
                            });
                        }
                        function round(num) {
                            return Math.round(num * 1000) / 1000;
                        }
                        // // TODO
                        var value = options.get('image');
                        var aspectRatio = options.get('aspectRatio');
                        if (value && (!aspectRatio || aspectRatio === DEFAULT_ASPECT_RATIO)) {
                            return loadImage(value).then(function (img) {
                                var possiblyUpdatedAspectRatio = options.get('aspectRatio');
                                if (options.get('image') === value &&
                                    (!possiblyUpdatedAspectRatio || possiblyUpdatedAspectRatio === DEFAULT_ASPECT_RATIO)) {
                                    if (img.width && img.height) {
                                        options.set('aspectRatio', round(img.height / img.width));
                                    }
                                }
                            });
                        }
                    },
                },
                {
                    name: 'posterImage',
                    type: 'file',
                    allowedFileTypes: ['jpeg', 'png'],
                    helperText: 'Image to show before the video plays',
                },
                {
                    name: 'autoPlay',
                    type: 'boolean',
                    defaultValue: true,
                },
                {
                    name: 'controls',
                    type: 'boolean',
                    defaultValue: false,
                },
                {
                    name: 'muted',
                    type: 'boolean',
                    defaultValue: true,
                },
                {
                    name: 'loop',
                    type: 'boolean',
                    defaultValue: true,
                },
                {
                    name: 'playsInline',
                    type: 'boolean',
                    defaultValue: true,
                },
                {
                    name: 'fit',
                    type: 'text',
                    defaultValue: 'cover',
                    enum: ['contain', 'cover', 'fill', 'auto'],
                },
                {
                    name: 'position',
                    type: 'text',
                    defaultValue: 'center',
                    enum: [
                        'center',
                        'top',
                        'left',
                        'right',
                        'bottom',
                        'top left',
                        'top right',
                        'bottom left',
                        'bottom right',
                    ],
                },
                {
                    name: 'height',
                    type: 'number',
                    hideFromUI: true,
                },
                {
                    name: 'width',
                    type: 'number',
                    hideFromUI: true,
                },
                {
                    name: 'aspectRatio',
                    type: 'number',
                    hideFromUI: true,
                    defaultValue: DEFAULT_ASPECT_RATIO,
                },
            ],
        })
    ], Video);
    return Video;
}(react_1.default.Component));
exports.Video = Video;
//# sourceMappingURL=Video.js.map