var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import * as React from 'react';
import { connect } from 'react-redux';
import styled from 'react-emotion';
import Renderer from '../../lib/object3dts/Renderer';
import { selectObject, deselectAllObjects } from '../../actions/threed';
import { getSelectedObjects } from '../../reducers/threed/';
import { Icon } from '@bitbloq/ui';
var Container = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n  flex: 1;\n  position: relative;\n  display: flex;\n  overflow: hidden;\n"], ["\n  flex: 1;\n  position: relative;\n  display: flex;\n  overflow: hidden;\n"])));
var StatusBar = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n  position: absolute;\n  height: 24px;\n  background-color: #5d6069;\n  bottom: 0px;\n  left: 0px;\n  right: 0px;\n  padding: 0px 20px;\n  display: flex;\n  align-items: center;\n  z-index: 10;\n  color: white;\n  font-size: 12px;\n"], ["\n  position: absolute;\n  height: 24px;\n  background-color: #5d6069;\n  bottom: 0px;\n  left: 0px;\n  right: 0px;\n  padding: 0px 20px;\n  display: flex;\n  align-items: center;\n  z-index: 10;\n  color: white;\n  font-size: 12px;\n"])));
var StatusBarGroup = styled.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n  display: flex;\n  margin-right: 40px;\n\n  b, span {\n    margin-right: 20px;\n  }\n  span:last-child {\n    margin-right; 0px;\n  }\n"], ["\n  display: flex;\n  margin-right: 40px;\n\n  b, span {\n    margin-right: 20px;\n  }\n  span:last-child {\n    margin-right; 0px;\n  }\n"])));
var CameraButtons = styled.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n  position: absolute;\n  top: 150px;\n  left: 20px;\n  z-index: 10;\n"], ["\n  position: absolute;\n  top: 150px;\n  left: 20px;\n  z-index: 10;\n"])));
var CameraButton = styled.div(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 33px;\n  height: 33px;\n  cursor: pointer;\n  margin-bottom: 10px;\n  background-color: white;\n  border-radius: 4px;\n  border: 1px solid #cfcfcf;\n\n  svg {\n    width: ", ";\n  }\n"], ["\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  width: 33px;\n  height: 33px;\n  cursor: pointer;\n  margin-bottom: 10px;\n  background-color: white;\n  border-radius: 4px;\n  border: 1px solid #cfcfcf;\n\n  svg {\n    width: ", ";\n  }\n"])), function (props) { return (props.wideIcon ? '18px' : '12px'); });
var ThreeDViewerState = /** @class */ (function () {
    function ThreeDViewerState() {
        this.selectedPosition = null;
        this.isOrthographic = false;
    }
    return ThreeDViewerState;
}());
var ThreeDViewer = /** @class */ (function (_super) {
    __extends(ThreeDViewer, _super);
    function ThreeDViewer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.rendererContainerRef = React.createRef();
        _this.state = new ThreeDViewerState();
        _this.onZoomIn = function () {
            _this.renderer.zoomIn();
        };
        _this.onZoomOut = function () {
            _this.renderer.zoomOut();
        };
        _this.onToggleOrthographic = function () {
            var isOrthographic = !_this.state.isOrthographic;
            _this.renderer.setOrtographicCamera(isOrthographic);
            _this.setState({ isOrthographic: isOrthographic });
        };
        return _this;
    }
    ThreeDViewer.prototype.componentDidUpdate = function (prevProps) {
        var _a = this.props, scene = _a.scene, sceneObjects = _a.sceneObjects, selectedObjects = _a.selectedObjects, activeOperation = _a.activeOperation;
        if (sceneObjects !== prevProps.sceneObjects) {
            this.renderer.updateScene();
        }
        if (selectedObjects !== prevProps.selectedObjects) {
            this.updateStatusBar();
        }
        this.renderer.setActiveHelper(activeOperation);
    };
    ThreeDViewer.prototype.componentDidMount = function () {
        var _this = this;
        var _a = this.props, scene = _a.scene, selectObject = _a.selectObject, deselectAllObjects = _a.deselectAllObjects;
        var container = this.rendererContainerRef.current;
        if (container) {
            this.renderer = new Renderer(scene, container);
            this.renderer.updateScene();
        }
        this.renderer.onObjectClick(function (object) {
            var _a = _this.props, controlPressed = _a.controlPressed, shiftPressed = _a.shiftPressed;
            selectObject(object, controlPressed || shiftPressed);
        });
        this.renderer.onBackgroundClick(function () { return deselectAllObjects(); });
    };
    ThreeDViewer.prototype.updateStatusBar = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, scene, selectedObjects, selectedPosition;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this.props, scene = _a.scene, selectedObjects = _a.selectedObjects;
                        this.setState({ selectedPosition: undefined });
                        if (!(selectedObjects && selectedObjects.length === 1)) return [3 /*break*/, 2];
                        return [4 /*yield*/, scene.getPositionAsync(selectedObjects[0])];
                    case 1:
                        selectedPosition = _b.sent();
                        this.setState({ selectedPosition: selectedPosition });
                        _b.label = 2;
                    case 2: return [2 /*return*/];
                }
            });
        });
    };
    ThreeDViewer.prototype.render = function () {
        var _a = this.state, selectedPosition = _a.selectedPosition, isOrthographic = _a.isOrthographic;
        return (React.createElement(Container, { innerRef: this.rendererContainerRef },
            React.createElement(CameraButtons, null,
                React.createElement(CameraButton, { onClick: this.onZoomIn },
                    React.createElement(Icon, { name: "plus" })),
                React.createElement(CameraButton, { onClick: this.onZoomOut },
                    React.createElement(Icon, { name: "minus" })),
                React.createElement(CameraButton, { onClick: this.onToggleOrthographic, wideIcon: true },
                    isOrthographic && React.createElement(Icon, { name: "perspective" }),
                    !isOrthographic && React.createElement(Icon, { name: "orthographic" }))),
            React.createElement(StatusBar, null,
                selectedPosition && (React.createElement(StatusBarGroup, null,
                    React.createElement("b", null, "Position (mm):"),
                    React.createElement("span", null,
                        "X=",
                        selectedPosition.position.x.toFixed(2)),
                    React.createElement("span", null,
                        "Y=",
                        selectedPosition.position.y.toFixed(2)),
                    React.createElement("span", null,
                        "Z=",
                        selectedPosition.position.z.toFixed(2)))),
                selectedPosition && (React.createElement(StatusBarGroup, null,
                    React.createElement("b", null, "Rotation (grads):"),
                    React.createElement("span", null,
                        "X=",
                        selectedPosition.angle.x.toFixed(2)),
                    React.createElement("span", null,
                        "Y=",
                        selectedPosition.angle.y.toFixed(2)),
                    React.createElement("span", null,
                        "Z=",
                        selectedPosition.angle.z.toFixed(2)))))));
    };
    return ThreeDViewer;
}(React.Component));
var mapStateToProps = function (state) { return ({
    scene: state.threed.scene.sceneInstance,
    sceneObjects: state.threed.scene.objects,
    activeOperation: state.threed.ui.activeOperation,
    selectedObjects: getSelectedObjects(state.threed),
    controlPressed: state.ui.controlPressed,
    shiftPressed: state.ui.shiftPressed,
}); };
var mapDispatchToProps = {
    selectObject: selectObject,
    deselectAllObjects: deselectAllObjects,
};
export default connect(mapStateToProps, mapDispatchToProps)(ThreeDViewer);
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5;
//# sourceMappingURL=ThreeDViewer.js.map