"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");
// import { BuilderElement } from '@builder.io/sdk'
var builder_block_component_1 = require("../components/builder-block.component");
var builder_block_decorator_1 = require("../decorators/builder-block.decorator");
var builder_meta_1 = require("../store/builder-meta");
var react_native_1 = require("react-native");
var DEFAULT_ASPECT_RATIO = 0.7041;
var Image = /** @class */ (function (_super) {
    __extends(Image, _super);
    function Image() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    // TODO
    // static universal: BuilderElement[] = []
    Image.prototype.render = function () {
        var _this = this;
        var _a = this.props, aspectRatio = _a.aspectRatio, builderBlock = _a.builderBlock;
        var children = this.props.builderBlock && this.props.builderBlock.children;
        return (
        // TODO: swap-in amp components hm
        // These styles may be bad... may need to remove this wrapper entirely hmm
        // <div style={{ position: 'relative', fontSize: 0 }}>
        react_1.default.createElement(builder_meta_1.BuilderMetaContext.Consumer, null, function (value) {
            return (react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(react_native_1.Image
                // alt={this.props.altText}
                // height={
                //   this.props.height || (aspectRatio ? Math.round(aspectRatio * 1000) : undefined)
                // }
                // width={
                //   this.props.width || (aspectRatio ? Math.round(1000 / aspectRatio) : undefined)
                // }
                // role={!this.props.altText ? 'presentation' : undefined}
                , { 
                    // alt={this.props.altText}
                    // height={
                    //   this.props.height || (aspectRatio ? Math.round(aspectRatio * 1000) : undefined)
                    // }
                    // width={
                    //   this.props.width || (aspectRatio ? Math.round(1000 / aspectRatio) : undefined)
                    // }
                    // role={!this.props.altText ? 'presentation' : undefined}
                    style: __assign({ resizeMode: _this.props.backgroundSize }, (aspectRatio && {
                        position: 'absolute',
                        height: '100%',
                        width: '100%',
                        left: 0,
                        top: 0,
                    })), resizeMode: _this.props.backgroundSize, 
                    // className="builder-image"
                    source: { uri: _this.props.image } }),
                aspectRatio ? (react_1.default.createElement(react_native_1.View, { pointerEvents: "none", 
                    // className="builder-image-sizer"
                    style: {
                        width: '100%',
                        paddingTop: aspectRatio * 100 + '%',
                    } })) : null,
                children && children.length ? (react_1.default.createElement(react_native_1.View, { style: {
                        display: 'flex',
                        flexDirection: 'column',
                        alignItems: 'stretch',
                        position: 'absolute',
                        top: 0,
                        left: 0,
                        width: '100%',
                        height: '100%',
                    } }, children.map(function (block, index) { return (react_1.default.createElement(builder_block_component_1.BuilderBlock, { key: block.id, block: block })); }))) : null));
        })
        // <div
        //   style={{
        //     position: 'absolute',
        //     top: 0,
        //     left: 0,
        //     right: 0,
        //     bottom: 0,
        //     backgroundImage: `url("${this.props.image}")`,
        //     backgroundSize: this.props.backgroundSize,
        //     backgroundRepeat: this.props.backgroundRepeat,
        //     backgroundPosition: this.props.backgroundPosition,
        //   }}
        //   className="builder-image"
        // />
        );
    };
    Image = __decorate([
        builder_block_decorator_1.BuilderBlock({
            name: 'Image',
            image: 'https://firebasestorage.googleapis.com/v0/b/builder-3b0a2.appspot.com/o/images%2Fbaseline-insert_photo-24px.svg?alt=media&token=4e5d0ef4-f5e8-4e57-b3a9-38d63a9b9dc4',
            defaultStyles: {
                minHeight: '20px',
                minWidth: '20px',
                overflow: 'hidden',
            },
            canHaveChildren: true,
            inputs: [
                {
                    name: 'image',
                    type: 'file',
                    // TODO: auto convert png to jpg when there is no transparency
                    allowedFileTypes: ['jpeg', 'jpg', 'png', 'svg'],
                    required: true,
                    // TODO: something better
                    defaultValue: 'https://builder.io/api/v1/image/assets%2Fpwgjf0RoYWbdnJSbpBAjXNRMe9F2%2Ffb27a7c790324294af8be1c35fe30f4d',
                    onChange: function (options) {
                        var DEFAULT_ASPECT_RATIO = 0.7041;
                        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));
                                        options.set('height', img.height);
                                        options.set('width', img.width);
                                    }
                                }
                            });
                        }
                    },
                },
                {
                    name: 'backgroundSize',
                    type: 'text',
                    defaultValue: 'cover',
                    enum: [
                        { label: 'contain', value: 'contain', helperText: 'The image should never get cropped' },
                        {
                            label: 'cover',
                            value: 'cover',
                            helperText: "The image should fill it's box, cropping when needed",
                        },
                    ],
                },
                {
                    name: 'backgroundPosition',
                    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',
                    helperText: "This is the ratio of height/width, e.g. set to 1.5 for a 300px wide and 200px tall photo. Set to 0 to not force the image to maintain it's aspect ratio",
                    advanced: true,
                    defaultValue: DEFAULT_ASPECT_RATIO,
                },
                {
                    name: 'altText',
                    type: 'string',
                    hideFromUI: true,
                    advanced: true,
                },
            ],
        })
    ], Image);
    return Image;
}(react_1.default.Component));
exports.Image = Image;
//# sourceMappingURL=Image.js.map