# Maximize over vector with conditional expression

I am unable to maximize a function containing an If statement and a vector variable. To give an MWE (not my original problem), consider the following:

x = {x1, x2};
Maximize[If[x[[2]] == 1, 1, -x[[1]]^2], x]


This returns no error, but returns

Maximize[If[x2 == 1, 1, -x[[1]]^2], {x1, x2}]


As you can see, it refuses to convert the second vector element to x1. If I do that manually by entering

x = {x1, x2};
Maximize[If[x[[2]] == 1, 1, -x1^2], x]


then it easily spits out

{1, {x1 -> 33/10, x2 -> 1}}


Can anybody offer me some insight into what's going on here? Is there a way to tweak it that doesn't involve manual rewriting? (If I have to type out all vector operations, it's going to be a long ugly formula...)

You'll want to use Piecewise[] to represent your function, Indexed[] to extract coordinates, and FullRegion[2] as the search domain. Thus:
Maximize[Piecewise[{{1, Indexed[x, 2] == 1}}, -Indexed[x, 1]^2], x ∈ FullRegion[2]]