Select group by layer name problem

Im new to UXP and scripting, i’ve tried to use deepseek to make the code for my panel but the function is not working. Can anyone help

Everything works as i would like it to except the visibility of the group is being changed to visible. I’d like the group to be selected without the visibility change.

// Function to select a layer by name
async function selectLayerByName(layerName) {
  const app = photoshop.app;
  const allLayers = app.activeDocument.layers;

  // Find all layers that match the name
  const layers = findAllLayersByName(allLayers, layerName);

  if (layers.length > 0) {
    try {
      // Execute the selection within a modal scope
      await photoshop.core.executeAsModal(async () => {
        // Get the current index for this layer name
        let currentIndex = currentIndices[layerName];

        // Select the next layer in the sequence
        const layer = layers[currentIndex];
        SelectLayer(layer);
        console.log(`Selected layer: ${layer.name}`);

        // Force a UI update without changing visibility
        // Temporarily deselect and reselect the layer
        layer.selected = false;
        await new Promise((resolve) => setTimeout(resolve, 50)); // Wait for the UI to update
        layer.selected = true;

        // Expand the layer group if it's a group
        if (layer.layers && layer.layers.length > 0) {
          layer.expanded = true; // Expand the group in the Layers panel
        }

        // Update the index for the next click
        currentIndices[layerName] = (currentIndex + 1) % layers.length;
      }, { "commandName": "Select Layer" });
    } catch (error) {
      console.error(`Failed to select layer: ${error.message}`);
    }
  } else {
    console.error(`No layers found with name "${layerName}".`);
  }
}

I believe you have the same issue as I did. Manipulating any layer property might be a have some unexpected behaviour. For context, go through this topic, but solution for me was this post: