// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package website

(@

load("@ytt:assert", "assert")
load("@ytt:data", "data")

def golang_escape_tick(str):
  # in Go the only way to escape tick is to
  # break up string and wrap tick in double quotes
  return str.replace("`", "`+\"`\"+`")
end

assetFiles = []
exampleFiles = []

for file in data.list():
  if file.startswith('js/') or file.startswith('templates/'):
    assetFiles.append(file)
  elif file.startswith('example'):
    exampleFiles.append(file)
  end
end

exampleSets = [
  { "id": "basics",
    "description": "Specific topics, by example.",
    "examples": [
      {"id": "example-plain-yaml"},
      {"id": "example-datatypes"},
      {"id": "example-variable"},
      {"id": "example-if"},
      {"id": "example-for"},
      {"id": "example-function"},
      {"id": "example-load"},
      {"id": "example-load-ytt-library-module"},
      {"id": "example-load-custom-library-module"},
      {"id": "example-assert"},
      {"id": "example-load-data-values"},
      {"id": "example-load-data-files"},
      {"id": "example-text-template"},
      {"id": "example-replace"},
      {"id": "example-overlay"},
      {"id": "example-overlay-files"},
      {"id": "example-multiple-data-values"},
      {"id": "example-ytt-library-module"},
      {"id": "example-k8s-ingress-single"},
      {"id": "example-k8s-ingress-multiple"},
      {"id": "example-k8s-helm-ish"},
      {"id": "example-demo"},
    ]
  },
  { "id": "overlays",
        "description": "Patching, in depth.",
        "examples": [
          {"id": "example-match-all-docs"},
          {"id": "example-match-subset-docs"},
          {"id": "example-match-subset-by-fragment"},
          {"id": "example-match-by-index"},
          {"id": "example-match-by-key"},
          {"id": "example-insert-array-item"},
          {"id": "example-replace-array-item"},
          {"id": "example-remove-array-item"},
          {"id": "example-edit-array-item"},
          {"id": "example-append-array-item"},
          {"id": "example-replace-array"},
          {"id": "example-add-map-item"},
          {"id": "example-edit-map-value"},
          {"id": "example-remove-map-item"},
          {"id": "example-rename-key-in-map"},
          {"id": "example-append-map-to-array"},
        ]
  },
  { "id": "getting-started",
    "description": "Step-by-step tutorial on using ytt well.",
    "examples": [
      {"id": "example-hello-world"},
      {"id": "example-extract-to-variables"},
      {"id": "example-extract-yaml-fragments"},
      {"id": "example-calc-in-functions"},
      {"id": "example-externalize-data-values"},
      {"id": "example-splitting-a-template"},
      {"id": "example-factor-to-modules"},
      {"id": "example-collect-to-packages"},
      {"id": "example-overlay-data-values"},
      {"id": "example-extend-data-values"},
      {"id": "example-overlay-on-templates"},
      {"id": "example-overlay-map-items"},
      {"id": "example-append-array-items"},
      {"id": "example-modify-array-items"},
    ]
  }
]

for group in exampleSets:
  for example in group["examples"]:
    example["files"] = []
    for file in exampleFiles:
      if file.startswith(example["id"]+"/"):
        example["files"].append(file)
      end
    end
    if len(example["files"]) == 0:
      assert.fail("expected to find at least one file for example "+example["id"])
    end
  end
end

unaccountedExampleFiles = []

for file in exampleFiles:
  accounted = False
  for group in exampleSets:
      for example in group["examples"]:
        for accountedFile in example["files"]:
          if accountedFile == file:
            accounted = True
            break
          end
        end
      end
  end
  if not accounted:
    unaccountedExampleFiles.append(file)
  end
end

if len(unaccountedExampleFiles) > 0:
  assert.fail("found unaccounted example files {}".format(unaccountedExampleFiles))
end

@)

func init() {
	Files = map[string]File{
		(@ for name in assetFiles: @)
    "(@= name @)": File{
      Name: "(@= name @)",
      Content: `(@= golang_escape_tick(data.read(name)) @)`,
    },
		(@ end @)
	}

  exampleSets = []exampleSet{
    (@ for group in exampleSets: @)
     {
      ID: "(@= group["id"] @)",
      DisplayName: "(@= group["id"].replace("-", " ").title() @)",
      Description: "(@= group["description"] @)",
      Examples: []Example{
        (@ for example in group["examples"]: @)
        {
          ID: "(@= example["id"] @)",
          DisplayName: "(@= example["id"].replace("example-", "").replace("-", " ").capitalize() @)",
          Files: []File{
            (@ for file in example["files"]: @)
            (@ if file.endswith("README.md"): @)(@ continue @)(@ end @)
            {
              Name: "(@= file.replace(example["id"]+"/", "", 1).replace("_ytt_lib1", "_ytt_lib") @)",
              Content: `(@= golang_escape_tick(data.read(file)) @)`,
            },
            (@ end @)
          },
        },
        (@ end @)
      },
    },
    (@ end @)
  }
}
