var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b ||= {})
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};

// packages/plugins/parking-layer/src/parkingLayer.ts
import {
  EdgesGeometry,
  ExtrudeGeometry,
  LineSegments,
  Mesh as Mesh2,
  Object3D as Object3D2,
  Shape
} from "three";

// packages/plugins/parking-layer/src/utils.ts
import {
  MeshLambertMaterial,
  MeshStandardMaterial,
  MeshPhysicalMaterial,
  MeshPhongMaterial,
  MeshBasicMaterial,
  LineBasicMaterial,
  SpriteMaterial,
  Vector2
} from "three";
var material = {
  MeshLambertMaterial,
  MeshStandardMaterial,
  MeshPhysicalMaterial,
  MeshPhongMaterial,
  MeshBasicMaterial,
  LineBasicMaterial,
  SpriteMaterial
};
var OBJECT_BASE_CONF = {
  floor: {
    name: "floor",
    material: "MeshLambertMaterial",
    color: "#fff",
    transparent: true,
    opacity: 1
  },
  park: {
    name: "park",
    material: "MeshLambertMaterial",
    color: "#44c3b4",
    transparent: true,
    opacity: 1
  },
  parkBorder: {
    name: "parkBorder",
    material: "LineBasicMaterial",
    color: "#f6ffed",
    transparent: true,
    opacity: 0.1
  },
  pillar: {
    name: "pillar",
    material: "MeshLambertMaterial",
    color: "#e7e7e7",
    transparent: true,
    opacity: 1
  },
  machine: {
    name: "machine",
    material: "SpriteMaterial"
  },
  connector: {
    name: "connector",
    material: "SpriteMaterial"
  }
};
var arrayPoints23Points = (points) => {
  const shapePoints = [];
  let point;
  for (let i = 0, len = points.length; i < len; i += 2) {
    point = new Vector2(+points[i], +points[i + 1]);
    shapePoints.push(point);
  }
  return shapePoints;
};
var deletePropertyForMaterial = (conf) => {
  const newConf = JSON.parse(JSON.stringify(conf));
  Reflect.deleteProperty(newConf, "material");
  Reflect.deleteProperty(newConf, "renderOrder");
  Reflect.deleteProperty(newConf, "depth");
  return newConf;
};
var renderOrderConf = [
  ["floor"],
  ["park"],
  ["parkBorder"],
  ["pillar"],
  ["machine", "connector"]
];
var getRenderOrderPlusByLevel = (level) => renderOrderConf.length * level;
var getRenderOrder = (name) => {
  const index = renderOrderConf.findIndex(
    (item) => item.find((itemj) => itemj === name)
  );
  return index > -1 ? index : 0;
};
var THEME = {
  normal: {
    floor: { color: "#484f5a" },
    park: { color: "#689f79" },
    parkBorder: { color: "#fff" },
    pillar: { color: "#fff" },
    machine: { color: "" },
    connector: { color: "" }
  }
};
var currTheme = THEME.normal;
for (const key of Object.keys(OBJECT_BASE_CONF)) {
  OBJECT_BASE_CONF[key].color = currTheme[key].color;
  OBJECT_BASE_CONF[key].renderOrder = getRenderOrder(key);
}

// packages/plugins/parking-layer/src/parkingLayer.ts
import { Element } from "@ktmap/map";
var ParkingLayer = class extends Element {
  constructor({ name = "", data, poiCollision = true }) {
    super();
    __publicField(this, "options");
    __publicField(this, "info", /* @__PURE__ */ new Map());
    __publicField(this, "floorIdSort", []);
    __publicField(this, "basicSetting", {
      floorHeight: 50,
      coordinateScale: 1
    });
    __publicField(this, "drawObjectStrategy", {
      floor: (data, renderOrderPlus, height) => {
        const zHeight = this.getZHeight(height);
        const item = data && data[0];
        if (!item)
          return [];
        const conf = OBJECT_BASE_CONF["floor" /* floor */];
        const newConf = deletePropertyForMaterial(conf);
        const mat = new material[conf.material](__spreadValues({}, newConf));
        const points = arrayPoints23Points(item.outline);
        const shape = new Shape(points);
        const mesh = new Mesh2();
        mesh.material = mat;
        const geo = new ExtrudeGeometry(
          shape,
          {
            depth: 1,
            bevelEnabled: false
          }
        );
        mesh.geometry = geo;
        mesh.name = "floor" /* floor */;
        mesh.userData = __spreadProps(__spreadValues({}, item), { zHeight });
        mesh.renderOrder = conf.renderOrder + renderOrderPlus;
        return [mesh];
      },
      park: (data, renderOrderPlus, height, zIndex) => {
        const zHeight = this.getZHeight(height);
        const item = data[0];
        if (!item)
          return [];
        const conf = OBJECT_BASE_CONF["park" /* park */];
        const newConf = deletePropertyForMaterial(conf);
        const mat = new material[conf.material](__spreadValues({}, newConf));
        let points;
        const meshs = [];
        for (const item2 of data) {
          points = arrayPoints23Points(item2.outline);
          const shape = new Shape(points);
          const mesh = new Mesh2();
          mesh.position.z = 2;
          const line = new LineSegments();
          line.position.z = mesh.position.z * 2 + 1;
          mesh.material = mat;
          const conf2 = OBJECT_BASE_CONF["parkBorder" /* parkBorder */];
          const newConf2 = deletePropertyForMaterial(conf2);
          const matLine = new material[conf2.material](newConf2);
          line.material = matLine;
          const geo = new ExtrudeGeometry(
            shape,
            {
              depth: 1,
              bevelEnabled: false
            }
          );
          mesh.geometry = geo;
          line.geometry = new EdgesGeometry(geo);
          mesh.name = "park" /* park */;
          line.renderOrder = conf2.renderOrder + renderOrderPlus;
          mesh.renderOrder = conf.renderOrder + renderOrderPlus;
          mesh.userData = {
            id: item2.id,
            type: item2.type,
            zHeight,
            zIndex,
            data: points
          };
          meshs.push(mesh, line);
        }
        return meshs;
      },
      pillar: (data, renderOrderPlus, height) => {
        const item = data[0];
        if (!item)
          return [];
        const zHeight = this.getZHeight(height);
        const conf = OBJECT_BASE_CONF["pillar" /* pillar */];
        const newConf = deletePropertyForMaterial(conf);
        const mat = new material[conf.material](__spreadValues({}, newConf));
        const meshs = [];
        const draw = (item2) => {
          const mesh = new Mesh2();
          const points = arrayPoints23Points(item2.outline);
          const shape = new Shape(points);
          mesh.material = mat;
          const geo = new ExtrudeGeometry(
            shape,
            {
              depth: zHeight,
              bevelEnabled: false
            }
          );
          mesh.geometry = geo;
          mesh.userData = __spreadValues({}, item2);
          mesh.name = "pillar" /* pillar */;
          mesh.renderOrder = conf.renderOrder + renderOrderPlus;
          meshs.push(mesh);
        };
        for (const item2 of data)
          draw(item2);
        return meshs;
      }
    });
    __publicField(this, "_nextTick", () => {
    });
    __publicField(this, "_updateSize", () => {
    });
    this.options = { name, data, poiCollision };
  }
  nextTick(cb) {
    if (cb) {
      this._nextTick = cb;
      return void 0;
    } else {
      return new Promise((resolve) => this._nextTick = resolve);
    }
  }
  __init() {
    return __async(this, null, function* () {
      if (typeof this.options.data === "function") {
        try {
          const res = yield this.options.data();
          const { coordinateScale, floorDatas } = res;
          this.floorIdSort = floorDatas.map((_) => _.sort).sort((a, b) => a - b);
          this.basicSetting.coordinateScale = coordinateScale;
          this.createFloors(floorDatas);
          this._nextTick();
          this.emitter.emit("complete");
        } catch (error) {
        }
      } else {
        console.log("this.options.data :>> ", this.options.data);
        const res = this.options.data;
        const { coordinateScale, floorDatas } = res;
        this.floorIdSort = floorDatas.map((_) => _.sort).sort((a, b) => a - b);
        this.basicSetting.coordinateScale = coordinateScale;
        this.createFloors(floorDatas);
        this._nextTick();
        this.emitter.emit("complete");
      }
    });
  }
  getZHeight(height) {
    return height * 1e3 / this.basicSetting.coordinateScale;
  }
  createFloorBefore(item) {
    const { id, sort, floorName, url, jsonData: extraInfo, data } = item;
    const height = {};
    Object.entries(extraInfo).forEach((item2) => {
      const [type, data2] = item2;
      height[type] = (data2 == null ? void 0 : data2.h) || 0;
    });
    const floorInfo = {
      id,
      floorName,
      height,
      sort,
      floorData: data
    };
    data.length && this.createFloor(floorInfo);
  }
  createFloors(data) {
    for (const item of data) {
      this.createFloorBefore(item);
    }
    const { reDraw, options } = this.map;
    reDraw();
    if (options.viewMode === "2D") {
      this.map.fitToBox2d();
    } else if (options.viewMode === "3D") {
      this.map.fitToBox3d();
    }
  }
  createFloor(floorInfo) {
    const { floorHeight, coordinateScale } = this.basicSetting;
    const { floorData, height, id, sort, floorName: name } = floorInfo;
    console.log("height :>> ", height);
    const zIndex = this.floorIdSort.indexOf(sort);
    const renderOrderPlus = getRenderOrderPlusByLevel(zIndex);
    const zHeight = zIndex * (floorHeight * 1e3) / coordinateScale;
    const dataFloor = floorData.filter(
      (_) => _.type === "floor" /* floor */
    );
    const meshFloors = this.drawObjectStrategy.floor(
      dataFloor,
      renderOrderPlus,
      1
    );
    const dataPark = floorData.filter((_) => _.type === "park" /* park */);
    const meshParks = this.drawObjectStrategy.park(
      dataPark,
      renderOrderPlus,
      1,
      zIndex
    );
    const dataPillar = floorData.filter(
      (_) => _.type === "pillar" /* pillar */
    );
    const meshPillars = this.drawObjectStrategy.pillar(
      dataPillar,
      renderOrderPlus,
      height["pillar" /* pillar */]
    );
    const floorGroup = new Object3D2();
    const conf = OBJECT_BASE_CONF["floor" /* floor */];
    meshFloors.length && floorGroup.add(...meshFloors);
    meshParks.length && floorGroup.add(...meshParks);
    meshPillars.length && floorGroup.add(...meshPillars);
    floorGroup.name = "level" /* level */;
    floorGroup.renderOrder = conf.renderOrder + renderOrderPlus;
    floorGroup.position.z = zHeight;
    floorGroup.userData = {
      id,
      name,
      zIndex,
      zHeight,
      sort
    };
    this.map.scene.add(floorGroup);
  }
};
__publicField(ParkingLayer, "NAME", "ParkingLayer");
export {
  ParkingLayer
};
//# sourceMappingURL=parking-layer.esm-bundler.js.map
