import { __awaiter, __generator } from "tslib";
import Instance from '../core/Instance';
import * as Cesium from '@msign/angularis';
var ClipControl = /** @class */ (function () {
    function ClipControl(viewer) {
        this._distance = -250;
        this._viewer = viewer;
    }
    ClipControl.prototype.createPlaneUpdateFunction = function (plane) {
        var _this = this;
        return function () {
            plane.distance = _this._distance;
            return plane;
        };
    };
    ClipControl.prototype.initClip = function (position, tileset, direction, distance) {
        return __awaiter(this, void 0, void 0, function () {
            var _viewer, clippingPlanes, transformCenter, height, i, plane;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        _viewer = this._viewer;
                        clippingPlanes = new Cesium.ClippingPlaneCollection({
                            planes: [
                                new Cesium.ClippingPlane(new Cesium.Cartesian3(direction.x, direction.y, direction.z), 0)
                            ],
                            edgeWidth: 1.0,
                            edgeColor: Cesium.Color.GREEN
                        });
                        tileset.clippingPlanes = clippingPlanes;
                        return [4 /*yield*/, tileset['readyPromise']];
                    case 1:
                        _a.sent();
                        if (!Cesium.Matrix4.equals(tileset.root.transform, Cesium.Matrix4.IDENTITY)) {
                            transformCenter = Cesium.Matrix4.getTranslation(tileset.root.transform, new Cesium.Cartesian3());
                            height = Cesium.Cartesian3.distance(transformCenter, position);
                            clippingPlanes.modelMatrix = Cesium.Matrix4.fromTranslation(new Cesium.Cartesian3(0.0, 0.0, height));
                        }
                        for (i = 0; i < clippingPlanes.length; ++i) {
                            plane = clippingPlanes.get(i);
                            _viewer.entities.add({
                                position: position,
                                plane: {
                                    dimensions: new Cesium.Cartesian2(600.0, 600.0),
                                    material: Cesium.Color.GREEN.withAlpha(0),
                                    plane: new Cesium.CallbackProperty(this.createPlaneUpdateFunction(plane), false),
                                    outline: false,
                                    outlineColor: Cesium.Color.BLACK
                                }
                            });
                        }
                        this.setClipDistance(distance);
                        return [2 /*return*/];
                }
            });
        });
    };
    ClipControl.prototype.setClipDistance = function (_distance) {
        return __awaiter(this, void 0, void 0, function () {
            var cSc;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        // 设置剖切距离 _distance: number
                        this._distance = _distance;
                        return [4 /*yield*/, Instance.getInstanceSceneControl()];
                    case 1:
                        cSc = _a.sent();
                        return [4 /*yield*/, cSc.forceRender(100)];
                    case 2:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    ClipControl.prototype.resetClip = function (tileset) {
        return __awaiter(this, void 0, void 0, function () {
            var _viewer;
            return __generator(this, function (_a) {
                _viewer = this._viewer;
                _viewer.entities.removeAll();
                tileset.clippingPlanes ? tileset.clippingPlanes.removeAll() : '';
                this._distance = 0.0;
                return [2 /*return*/];
            });
        });
    };
    return ClipControl;
}());
export default ClipControl;
