import { __awaiter, __extends, __generator } from "tslib";
import { throttle } from 'lodash';
import { Cartesian3 } from '@taotieh/cesium_extend';
import { TIMESPAN_INSTANT } from '../../../core/Constant';
import ClipWithPlane from './ClipWithPlane';
var State;
(function (State) {
    State[State["READY"] = 0] = "READY";
    State[State["POINT_ORIGIN_SELECTED"] = 1] = "POINT_ORIGIN_SELECTED";
    State[State["POINT_ORIGIN_CHANGED"] = 2] = "POINT_ORIGIN_CHANGED";
    State[State["DONE"] = 3] = "DONE";
})(State || (State = {}));
/**
 * 使用强制绑定为竖直方向的面刨切
 *
 * 类提供在水平方向改变面朝向的能力
 */
var ClipWithVertical = /** @class */ (function (_super) {
    __extends(ClipWithVertical, _super);
    function ClipWithVertical(viewer, instance) {
        var _this = 
        /** **********************************************************
         *  父类构造需要指定原点和法线, 这里的值无意义. 等待后续鼠标动作赋值
         *************************************************************/
        _super.call(this, viewer, instance, Cartesian3.ZERO.clone(), Cartesian3.UNIT_Y.clone()) || this;
        _this._onMove = throttle(function (evt) { return __awaiter(_this, void 0, void 0, function () {
            var x, y, _a, lastMovePoint, plane, deduceX, deduceY, distanceMap;
            return __generator(this, function (_b) {
                if (this._isPanning) {
                    x = evt.layerX, y = evt.layerY;
                    _a = this, lastMovePoint = _a._lastMovePoint, plane = _a._selectedPlane;
                    deduceX = lastMovePoint.x - x;
                    deduceY = lastMovePoint.y - y;
                    distanceMap = _super.prototype.getDistance.call(this);
                    if (Math.abs(distanceMap[plane.id.id]) < 10 && Math.abs(deduceY) < Math.abs(deduceX)) {
                        this._updateCuttingPlaneWithOrientation(deduceX / 10);
                    }
                    else {
                        this._updateCutingPlaneWithOrigin({ distance: deduceY, id: plane.id.id });
                    }
                    this._lastMovePoint = { x: x, y: y };
                }
                return [2 /*return*/];
            });
        }); }, TIMESPAN_INSTANT);
        _this._toReset();
        return _this;
    }
    ClipWithVertical.prototype._toSelectOrigin = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, origin, normal, entity;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, _super.prototype._toReset.call(this)];
                    case 1:
                        _b.sent();
                        _a = this, origin = _a._origin, normal = _a._normal;
                        return [4 /*yield*/, this._selectPoint(origin)];
                    case 2:
                        _b.sent();
                        this._state = State.POINT_ORIGIN_SELECTED;
                        return [4 /*yield*/, this._createCuttingPlaneWithOrigin(Cartesian3.negate(new Cartesian3(1, 1, 0), normal), origin)];
                    case 3:
                        entity = _b.sent();
                        return [4 /*yield*/, this.createRotateHandler(Cartesian3.negate(new Cartesian3(1, 1, 0), normal), origin, 
                            // @ts-ignore
                            entity)];
                    case 4:
                        _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    ClipWithVertical.prototype._toReset = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this._state = State.READY;
                        return [4 /*yield*/, _super.prototype._toReset.call(this)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    ClipWithVertical.prototype.start = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                _super.prototype.start.call(this);
                this._toReset();
                this.next();
                return [2 /*return*/];
            });
        });
    };
    ClipWithVertical.prototype.next = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (this._state) {
                    case State.READY:
                        this._toSelectOrigin();
                        break;
                    default:
                        this._toReset();
                }
                return [2 /*return*/];
            });
        });
    };
    ClipWithVertical.prototype.stop = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this._toReset()];
                    case 1:
                        _a.sent();
                        _super.prototype.stop.call(this);
                        return [2 /*return*/];
                }
            });
        });
    };
    return ClipWithVertical;
}(ClipWithPlane));
export default ClipWithVertical;
