/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
const p = [
  { name: "blue", fill: [0, 121, 193], outline: [0, 94, 149] },
  { name: "red", fill: [222, 41, 0], outline: [171, 60, 22] },
  { name: "orange", fill: [209, 126, 33], outline: [173, 101, 17] },
  { name: "purple", fill: [144, 129, 188], outline: [116, 97, 168] },
  { name: "green", fill: [90, 147, 89], outline: [51, 128, 51] },
  { name: "black", fill: [89, 89, 89], outline: [50, 50, 50] }
];
function f(n = 0) {
  const e = (n % p.length + p.length) % p.length, t = p[e];
  return {
    fill: [...t.fill],
    outline: [...t.outline]
  };
}
function g(n = 0) {
  const { fill: e, outline: t } = f(n), i = [...e, 153], r = [...t, 255];
  return {
    renderer: {
      type: "simple",
      symbol: {
        type: "CIMSymbolReference",
        symbol: {
          type: "CIMPointSymbol",
          symbolLayers: [
            {
              type: "CIMVectorMarker",
              enable: !0,
              name: "Fill",
              anchorPointUnits: "Relative",
              dominantSizeAxis3D: "Y",
              size: 12,
              billboardMode3D: "FaceNearPlane",
              frame: {
                xmin: -5,
                ymin: -5,
                xmax: 5,
                ymax: 5
              },
              markerGraphics: [
                {
                  type: "CIMMarkerGraphic",
                  geometry: {
                    rings: [
                      [
                        [
                          0,
                          5
                        ],
                        [
                          0.87,
                          4.92
                        ],
                        [
                          1.71,
                          4.7
                        ],
                        [
                          2.5,
                          4.33
                        ],
                        [
                          3.21,
                          3.83
                        ],
                        [
                          3.83,
                          3.21
                        ],
                        [
                          4.33,
                          2.5
                        ],
                        [
                          4.7,
                          1.71
                        ],
                        [
                          4.92,
                          0.87
                        ],
                        [
                          5,
                          0
                        ],
                        [
                          4.92,
                          -0.87
                        ],
                        [
                          4.7,
                          -1.71
                        ],
                        [
                          4.33,
                          -2.5
                        ],
                        [
                          3.83,
                          -3.21
                        ],
                        [
                          3.21,
                          -3.83
                        ],
                        [
                          2.5,
                          -4.33
                        ],
                        [
                          1.71,
                          -4.7
                        ],
                        [
                          0.87,
                          -4.92
                        ],
                        [
                          0,
                          -5
                        ],
                        [
                          -0.87,
                          -4.92
                        ],
                        [
                          -1.71,
                          -4.7
                        ],
                        [
                          -2.5,
                          -4.33
                        ],
                        [
                          -3.21,
                          -3.83
                        ],
                        [
                          -3.83,
                          -3.21
                        ],
                        [
                          -4.33,
                          -2.5
                        ],
                        [
                          -4.7,
                          -1.71
                        ],
                        [
                          -4.92,
                          -0.87
                        ],
                        [
                          -5,
                          0
                        ],
                        [
                          -4.92,
                          0.87
                        ],
                        [
                          -4.7,
                          1.71
                        ],
                        [
                          -4.33,
                          2.5
                        ],
                        [
                          -3.83,
                          3.21
                        ],
                        [
                          -3.21,
                          3.83
                        ],
                        [
                          -2.5,
                          4.33
                        ],
                        [
                          -1.71,
                          4.7
                        ],
                        [
                          -0.87,
                          4.92
                        ],
                        [
                          0,
                          5
                        ]
                      ]
                    ]
                  },
                  symbol: {
                    type: "CIMPolygonSymbol",
                    symbolLayers: [
                      {
                        type: "CIMSolidStroke",
                        enable: !0,
                        name: "Outline",
                        capStyle: "Round",
                        joinStyle: "Round",
                        lineStyle3D: "Strip",
                        miterLimit: 4,
                        width: 1.5,
                        height3D: 1,
                        anchor3D: "Center",
                        color: r
                      },
                      {
                        type: "CIMSolidFill",
                        enable: !0,
                        name: "Fill",
                        color: i
                      }
                    ],
                    angleAlignment: "Map"
                  }
                }
              ],
              scaleSymbolsProportionally: !0,
              respectFrame: !0,
              rotation: 360
            },
            {
              type: "CIMVectorMarker",
              enable: !0,
              name: "WhiteOutline",
              colorLocked: !0,
              anchorPointUnits: "Relative",
              dominantSizeAxis3D: "Y",
              size: 15.75,
              billboardMode3D: "FaceNearPlane",
              frame: {
                xmin: -5,
                ymin: -5,
                xmax: 5,
                ymax: 5
              },
              markerGraphics: [
                {
                  type: "CIMMarkerGraphic",
                  geometry: {
                    rings: [
                      [
                        [
                          0,
                          5
                        ],
                        [
                          0.87,
                          4.92
                        ],
                        [
                          1.71,
                          4.7
                        ],
                        [
                          2.5,
                          4.33
                        ],
                        [
                          3.21,
                          3.83
                        ],
                        [
                          3.83,
                          3.21
                        ],
                        [
                          4.33,
                          2.5
                        ],
                        [
                          4.7,
                          1.71
                        ],
                        [
                          4.92,
                          0.87
                        ],
                        [
                          5,
                          0
                        ],
                        [
                          4.92,
                          -0.87
                        ],
                        [
                          4.7,
                          -1.71
                        ],
                        [
                          4.33,
                          -2.5
                        ],
                        [
                          3.83,
                          -3.21
                        ],
                        [
                          3.21,
                          -3.83
                        ],
                        [
                          2.5,
                          -4.33
                        ],
                        [
                          1.71,
                          -4.7
                        ],
                        [
                          0.87,
                          -4.92
                        ],
                        [
                          0,
                          -5
                        ],
                        [
                          -0.87,
                          -4.92
                        ],
                        [
                          -1.71,
                          -4.7
                        ],
                        [
                          -2.5,
                          -4.33
                        ],
                        [
                          -3.21,
                          -3.83
                        ],
                        [
                          -3.83,
                          -3.21
                        ],
                        [
                          -4.33,
                          -2.5
                        ],
                        [
                          -4.7,
                          -1.71
                        ],
                        [
                          -4.92,
                          -0.87
                        ],
                        [
                          -5,
                          0
                        ],
                        [
                          -4.92,
                          0.87
                        ],
                        [
                          -4.7,
                          1.71
                        ],
                        [
                          -4.33,
                          2.5
                        ],
                        [
                          -3.83,
                          3.21
                        ],
                        [
                          -3.21,
                          3.83
                        ],
                        [
                          -2.5,
                          4.33
                        ],
                        [
                          -1.71,
                          4.7
                        ],
                        [
                          -0.87,
                          4.92
                        ],
                        [
                          0,
                          5
                        ]
                      ]
                    ]
                  },
                  symbol: {
                    type: "CIMPolygonSymbol",
                    symbolLayers: [
                      {
                        type: "CIMSolidFill",
                        enable: !0,
                        name: "WhiteOutline",
                        color: [
                          255,
                          255,
                          255,
                          153
                        ]
                      }
                    ],
                    angleAlignment: "Map"
                  }
                }
              ],
              scaleSymbolsProportionally: !0,
              respectFrame: !0,
              rotation: 360
            }
          ],
          haloSize: 0.75,
          scaleX: 1,
          angleAlignment: "Display",
          angle: 360
        }
      }
    }
  };
}
function L(n = 0) {
  const { fill: e } = f(n);
  return {
    renderer: {
      type: "simple",
      symbol: {
        type: "CIMSymbolReference",
        symbol: {
          type: "CIMLineSymbol",
          symbolLayers: [
            {
              type: "CIMSolidStroke",
              enable: !0,
              name: "Fill",
              capStyle: "Round",
              joinStyle: "Miter",
              lineStyle3D: "Strip",
              miterLimit: 4,
              width: 4.5,
              height3D: 1,
              anchor3D: "Center",
              color: [...e, 255]
            },
            {
              type: "CIMSolidStroke",
              enable: !0,
              name: "Outline",
              colorLocked: !0,
              capStyle: "Round",
              joinStyle: "Miter",
              lineStyle3D: "Strip",
              miterLimit: 4,
              width: 6,
              height3D: 1,
              anchor3D: "Center",
              color: [
                255,
                255,
                255,
                153
              ]
            }
          ]
        }
      }
    }
  };
}
function F(n = 0) {
  const { fill: e, outline: t } = f(n), i = [...e, 153];
  return {
    renderer: {
      type: "simple",
      symbol: {
        type: "CIMSymbolReference",
        symbol: {
          type: "CIMPolygonSymbol",
          symbolLayers: [
            {
              type: "CIMSolidStroke",
              enable: !0,
              name: "Outline",
              capStyle: "Round",
              joinStyle: "Round",
              lineStyle3D: "Strip",
              miterLimit: 4,
              width: 1.5,
              height3D: 1,
              anchor3D: "Center",
              color: [...t, 255]
            },
            {
              type: "CIMSolidFill",
              enable: !0,
              name: "Fill",
              color: i
            }
          ],
          angleAlignment: "Map"
        }
      }
    }
  };
}
class y {
  constructor(e) {
    this.llmFsLookup = {}, this.rendererColorCounters = {
      esriGeometryPoint: 0,
      esriGeometryPolyline: 0,
      esriGeometryPolygon: 0
    }, this.structure = e, this.buildSolution = this.structure?.buildSolution;
  }
  static {
    this.typeLookup = {
      point: "esriGeometryPoint",
      polyline: "esriGeometryPolyline",
      polygon: "esriGeometryPolygon"
    };
  }
  static {
    this.simpleTypeLookup = {
      esriGeometryPoint: "Point",
      esriGeometryPolyline: "Polyline",
      esriGeometryPolygon: "Polygon"
    };
  }
  static {
    this.drawingToolLookup = {
      esriGeometryPoint: "esriFeatureEditToolPoint",
      esriGeometryPolyline: "esriFeatureEditToolLine",
      esriGeometryPolygon: "esriFeatureEditToolPolygon",
      Table: "esriFeatureEditToolNone"
    };
  }
  static {
    this.fieldTypeToEsri = {
      string: "esriFieldTypeString",
      integer: "esriFieldTypeInteger",
      double: "esriFieldTypeDouble",
      date: "esriFieldTypeDate",
      guid: "esriFieldTypeGUID",
      globalid: "esriFieldTypeGlobalID",
      oid: "esriFieldTypeOID"
    };
  }
  static {
    this.fieldTypeToSimple = Object.fromEntries(
      Object.entries(y.fieldTypeToEsri).map(([e, t]) => [t, e])
    );
  }
  dumpToLlmTemplate() {
    const e = this.structure?.buildSolution?.solution, t = Object.entries(e?.strings || {}).map(([o, l]) => ({
      key: o,
      value: l.value,
      description: l.description
    })), i = {
      title: e?.title,
      description: e?.description,
      tags: e?.tags,
      strings: t
    }, r = this.structure?.buildSolution?.items, s = Object.entries(r || {}).map(([o, l]) => ({
      id: o,
      title: l.title,
      description: l.description,
      type: l.type
    }));
    let a = null;
    for (const [o, l] of Object.entries(r || {})) {
      if (l.type !== "Feature Layer (hosted)")
        continue;
      const u = [];
      for (const d of l.service?.layers || []) {
        const c = this.getFields(d), m = {
          id: d.id,
          type: "",
          typeMutable: !0,
          name: d.name,
          customFields: [],
          requiredFields: c,
          systemFields: []
        };
        d.geometryType && (m.type = y.simpleTypeLookup[d.geometryType] || "", m.typeMutable = !1), u.push(m);
      }
      for (const d of l.service?.tables || []) {
        const c = this.getFields(d);
        u.push({
          id: d.id,
          type: "Table",
          typeMutable: !1,
          name: d.name,
          customFields: [],
          requiredFields: c,
          systemFields: []
        });
      }
      a = {
        itemId: o,
        addLayers: l.addLayers,
        layers: u
      };
      break;
    }
    return {
      solution: i,
      items: s,
      featureLayer: a ?? {
        itemId: "",
        addLayers: !1,
        layers: []
      }
    };
  }
  getFields(e) {
    const t = [];
    for (const i of e.fields || [])
      t.push({
        name: i.name,
        type: y.fieldTypeToSimple[i.type] || i.type,
        alias: i.alias,
        description: y.getFieldDescription(i.description),
        choiceList: y.getFieldDomain(i.domain),
        length: i.length
      });
    return t;
  }
  static getFieldDescription(e) {
    if (e)
      try {
        return JSON.parse(e).value || e;
      } catch {
        return e;
      }
  }
  static getFieldDomain(e) {
    if (e)
      return e.codedValues?.map((t) => t.code);
  }
  applyLlmTemplate(e) {
    const t = this.buildSolution.solution;
    if (this.resetRendererColorCounters(), this.llmFsLookup = {}, e.featureLayer?.itemId) {
      this.llmFsLookup[e.featureLayer.itemId] = {};
      for (const o of e.featureLayer.layers)
        this.llmFsLookup[e.featureLayer.itemId][o.id] = o;
    }
    for (const [o, l] of Object.entries(this.buildSolution.items))
      l.type === "Feature Layer (hosted, view)" && (this.llmFsLookup[o] = { ...this.llmFsLookup[l.source] });
    t.title = e.solution.title, t.description = e.solution.description, t.tags = e.solution.tags;
    const i = t.strings;
    for (const o of e.solution.strings)
      i[o.key] && (i[o.key].value = o.value);
    const r = {}, s = /* @__PURE__ */ new Set(), a = this.buildSolution.items;
    for (const o of e.items) {
      const l = a[o.id];
      if (l)
        if (l.title = o.title, l.description = o.description, l.type === "Web Map")
          this.applyWebMap(l);
        else if (l.type === "Feature Layer (hosted)") {
          this.applyFeatureService(l, o.id, !1);
          const u = I(l);
          l.data = u, r[o.id] = u;
        } else l.type === "Feature Layer (hosted, view)" && (this.applyFeatureService(l, o.id, !0), s.add(o.id));
    }
    for (const o of s) {
      const l = a[o];
      l.data = r[l.source];
    }
    return this.structure;
  }
  applyWebMap(e) {
    if (!e.layerSources)
      return;
    for (const r of [...e.layers || [], ...e.tables || []]) {
      const [s, a] = this.extractIdAndLayerId(r.url), o = this.llmFsLookup[s]?.[a];
      o && (r.title = o.name);
    }
    if (!(e.layers?.length === 0 && e.tables?.length === 0))
      return;
    const i = crypto.randomUUID().replace(/-/gu, "").substring(0, 10);
    for (const r of e.layerSources) {
      const s = this.llmFsLookup[r];
      if (s)
        for (const [a, o] of Object.entries(s)) {
          const l = Number(a), u = o.type.toLowerCase() === "table", d = {
            id: `${i}-layer-${l}`,
            title: o.name,
            url: `{{${r}.layer${l}.url}}`,
            itemId: `{{${r}.layer${l}.itemId}}`
          };
          u ? e.tables.unshift(d) : (d.layerType = "ArcGISFeatureLayer", e.layers.unshift(d));
        }
    }
  }
  extractIdAndLayerId(e) {
    const t = e.replace(/[{}]/gu, "").split("."), i = t[0], r = Number.parseInt(t[1].replace("layer", ""));
    return [i, r];
  }
  applyFeatureService(e, t, i = !1) {
    const r = e.source || t, s = this.llmFsLookup[r];
    for (const o of [...e.service.layers, ...e.service.tables])
      this.applyLayer(o, s, i);
    e.addLayers && !i && this.addNewLayers(e, s);
    const a = e.service.layers?.length === 0 && e.service.tables?.length === 0;
    i && a && this.addNewViewLayers(e, t, s);
  }
  addNewLayers(e, t) {
    for (const [i, r] of Object.entries(t)) {
      const a = { id: Number(i) };
      this.applyLayer(a, t), r.type.toLowerCase() === "table" ? e.service.tables.push(a) : e.service.layers.push(a);
    }
  }
  addNewViewLayers(e, t, i) {
    const r = e.source;
    for (const [s, a] of Object.entries(i)) {
      const o = Number(s), l = a.type.toLowerCase() === "table", u = {
        adminLayerInfo: {
          geometryField: l ? null : { name: "Shape" },
          viewLayerDefinition: {
            sourceServiceName: `{{${r}.name}}`,
            sourceLayerId: o,
            sourceLayerFields: "*"
          }
        },
        id: o,
        name: a.name,
        extent: `{{${t}.solutionExtent}}`,
        maxRecordCount: 2e3
      };
      l ? e.service.tables.push(u) : e.service.layers.push(u);
    }
  }
  applyLayer(e, t, i = !1) {
    const r = t[e.id];
    if (!r || (e.name = r.name, e.description = r.description || "", i))
      return;
    const s = y.typeLookup[r.type.toLowerCase()] || r.type;
    s.toLowerCase() === "table" ? e.type = s : e.geometryType = s, e.editFieldsInfo = this.editFieldsInfo(), e.globalIdField = "globalid", e.objectIdField = "objectid", e.uniqueIdField = { name: "objectid", isSystemMaintained: !0 }, e.hasAttachments = !0, this.updateRelationships(e, t), this.updateFields(e, r), this.updateTemplates(e), this.updateDrawingInfo(e);
  }
  updateRelationships(e, t) {
    const i = e.relationships || [];
    for (const r of i) {
      const s = t[r.relatedTableId];
      s && (r.name = s.name);
    }
  }
  updateFields(e, t) {
    const i = [], r = [
      [!1, t.customFields],
      [!0, t.requiredFields],
      [!0, t.systemFields]
    ];
    for (const [s, a] of r)
      if (Array.isArray(a))
        for (const o of a) {
          const l = {
            name: o.name,
            alias: o.alias,
            type: y.fieldTypeToEsri[o.type] || o.type,
            description: JSON.stringify({ value: o.description, fieldValueType: "" }, null, 0),
            domain: this.createDomain(o.choiceList, o.name),
            required: s
          };
          o.type?.toLowerCase() === "string" && (l.length = o.length ?? 256), (o.type === "oid" || o.type === "globalid") && (l.editable = !1, l.nullable = !1), i.push(l);
        }
    e.fields = i;
  }
  updateTemplates(e) {
    const t = y.drawingToolLookup[e.geometryType || e.type];
    e.templates ??= [{
      name: e.name,
      description: "",
      prototype: { attributes: {} }
    }];
    for (const i of e.templates)
      i.drawingTool = t;
  }
  updateDrawingInfo(e) {
    if (e.geometryType === "esriGeometryPoint")
      e.drawingInfo = g(this.nextRendererColorIndex(e.geometryType));
    else if (e.geometryType === "esriGeometryPolyline")
      e.drawingInfo = L(this.nextRendererColorIndex(e.geometryType));
    else if (e.geometryType === "esriGeometryPolygon")
      e.drawingInfo = F(this.nextRendererColorIndex(e.geometryType));
    else
      return;
  }
  nextRendererColorIndex(e) {
    const t = this.rendererColorCounters[e];
    return t === void 0 ? 0 : (this.rendererColorCounters[e] = t + 1, t);
  }
  resetRendererColorCounters() {
    this.rendererColorCounters = {
      esriGeometryPoint: 0,
      esriGeometryPolyline: 2,
      esriGeometryPolygon: 4
    };
  }
  createDomain(e, t) {
    return e ? {
      type: "codedValue",
      name: `${t}_${crypto.randomUUID().replace(/-/gu, "")}`,
      codedValues: e.map((i) => ({ name: i, code: i }))
    } : null;
  }
  editFieldsInfo() {
    return {
      creationDateField: "created_date",
      creatorField: "created_user",
      editDateField: "last_edited_date",
      editorField: "last_edited_user"
    };
  }
}
function I(n) {
  const e = n.service, t = {
    layers: [],
    tables: []
  };
  for (const i of e.layers || [])
    t.layers.push({
      id: i.id,
      formInfo: h(i),
      popupInfo: b(i),
      layerDefinition: i.drawingInfo
    });
  for (const i of e.tables || [])
    t.tables.push({
      id: i.id,
      formInfo: h(i),
      popupInfo: b(i)
    });
  return t;
}
function h(n) {
  const e = [];
  for (const i of n.fields)
    i.required || e.push({
      label: i.alias,
      type: "field",
      editableExpression: "expr/system/true",
      fieldName: i.name,
      inputType: S(i.type, i.domain, i.length)
    });
  const t = n.relationships || [];
  for (const i of t)
    e.push({
      description: "",
      label: i.name,
      type: "relationship",
      displayCount: 3,
      displayType: "list",
      editableExpression: "expr/system/true",
      orderByFields: [{ field: "created_date", order: "asc" }],
      relationshipId: i.id
    });
  return {
    formElements: e,
    expressionInfos: [
      { expression: "false", name: "expr/system/false", returnType: "boolean", title: "False" },
      { expression: "true", name: "expr/system/true", returnType: "boolean", title: "True" }
    ],
    title: n.name
  };
}
function b(n) {
  const e = [], t = [];
  for (const r of n.fields)
    r.required || t.push({
      fieldName: r.name,
      isEditable: !0,
      label: r.alias,
      visible: !0
    });
  e.push({
    type: "fields",
    description: "",
    fieldInfos: t
  });
  const i = n.relationships || [];
  if (i.length && n.type !== "Table")
    for (const r of i)
      e.push({
        type: "relationship",
        description: "",
        displayCount: 3,
        displayType: "list",
        orderByFields: [{ field: "created_date", order: "asc" }],
        relationshipId: r.id
      });
  return e.push({
    type: "attachments",
    description: "",
    displayType: "auto",
    title: ""
  }), {
    popupElements: e,
    showLastEditInfo: !1,
    title: n.name
  };
}
function S(n, e, t) {
  return n === "esriFieldTypeString" && !e && t && t > 256 ? { type: "text-area", maxLength: t } : n === "esriFieldTypeString" && !e ? { type: "text-box", maxLength: t || 256, minLength: 0 } : n === "esriFieldTypeString" && e ? { type: "combo-box", noValueOptionLabel: "No value", showNoValueOption: !0 } : ["esriFieldTypeInteger", "esriFieldTypeDouble"].includes(n) ? { type: "text-box", minLength: 0 } : n === "esriFieldTypeDate" ? { type: "datetime-picker", includeTime: !1 } : { type: "text-box", maxLength: t || 256, minLength: 0 };
}
export {
  y as C
};
