import AttributeContent from "../AttributeContent";
import SectionModel from "../../content/SectionModel";

const json = {
  header: {
    label: "Header label",
    description: {
      message: "Dit is de beschrijving",
    },
  },
  label: "Blaastest geslaagd",
  elements: [
    {
      propertyElement: {
        label: "Properties",
        properties: [
          {
            label: "Gemiddelde",
            value: "80%",
          },
        ],
      },
    },
    {
      propertyElement: {
        label: "Second Properties",
        properties: [
          {
            label: "Second Gemiddelde",
            value: "20%",
          },
        ],
      },
    },
    {
      textFragmentElement: {
        label: "Description TextFragment",
        textfragments: [
          { id: "Description", text: { message: "Dit is een text fragment" } },
        ],
      },
    },
    {
      contentElement: {
        label: "Description Content",
        sections: [
          {
            id: "Alert.:-",
            type: "Reference",
            label: "Alert",
            number: "1",
            body: "<p>The limit of 0.5 per mille has been exceeded.</p>",
            _links: {
              self: {
                href: "/content/_Context/Police%20incident%20decisions/Content/Breath%20analysis/Breathalyzer%20test.formalsource/Alert.%3A-",
              },
            },
            sections: [
              {
                id: "pass",
                label: "Pass",
                body: "<p>Nog een sectie voor optie Pass.</p>",
                _links: {
                  self: {
                    href: "/content/Incident/Business design/Content/Blaastest.textilesource/pass",
                  },
                },
              },
              {
                id: "pass_alert",
                label: "Pass / Alert",
                body: "<p>Nog een sectie voor optie Pass / Alert.</p>",
                _links: {
                  self: {
                    href: "/content/Incident/Business design/Content/Blaastest.textilesource/pass_alert",
                  },
                },
              },
            ],
            subSections: [
              {
                id: "Pass",
                body: "<p>De blaastest heeft geen alcohol constateerd (versie 2017)</p>",
                _links: {
                  self: {
                    href: "/content/Incident/Business design/Content/Blaastest subsections.formalsource/BlaastestOpties#Pass",
                  },
                },
                subSections: [
                  {
                    id: "PassSubSection",
                    referred: true,
                    body: "<p>Sub sub section</p>",
                    _links: {
                      self: {
                        href: "/content/Incident/Business design/Content/Blaastest subsections.formalsource/BlaastestOpties#PassSubSection",
                      },
                    },
                    subSections: [
                      {
                        id: "PassSubSubSection",
                        body: "<p>Sub sub sub section</p>",
                        _links: {
                          self: {
                            href: "/content/Incident/Business design/Content/Blaastest subsections.formalsource/BlaastestOpties#PassSubSubSection",
                          },
                        },
                        subSections: [
                          {
                            id: "PassSubSubSubSection",
                            referred: true,
                            body: "<p>Sub sub sub sub section</p>",
                            _links: {
                              self: {
                                href: "/content/Incident/Business design/Content/Blaastest subsections.formalsource/BlaastestOpties#PassSubSubSubSection",
                              },
                            },
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
              {
                id: "PassAlert",
                referred: true,
                body: "<p>De limiet voor beginnende bestuurders is overschreden (0,2 promille). (versie 2017)</p>",
                _links: {
                  self: {
                    href: "/content/Incident/Business design/Content/Blaastest subsections.formalsource/BlaastestOpties#PassAlert",
                  },
                },
              },
            ],
          },
        ],
      },
    },
  ],
};

describe("AttributeContent", () => {
  it("Handles basic content elements", () => {
    const content = new AttributeContent(json);
    expect(content.label).toBe("Blaastest geslaagd");
    expect(content.header.label).toBe("Header label");
    expect(content.header.description).toBe("Dit is de beschrijving");
    expect(content.properties).toHaveLength(2);
    expect(content.textfragments).toHaveLength(1);
    expect(content.sections).toHaveLength(1);

    const firstProperty = content.properties[0];
    expect(firstProperty.label).toBe("Gemiddelde");
    expect(firstProperty.value).toBe("80%");

    const firstTextFragment = content.textfragments[0];
    expect(firstTextFragment.text).toBe("Dit is een text fragment");
  });

  it("Can ask for elements by type", () => {
    const content = new AttributeContent(json);

    expect(content.getConceptPropertiesByLabels("not-existing")).toHaveLength(
      0,
    );
    expect(content.getTextFragmentsByLabels("not-existing")).toHaveLength(0);
    expect(content.getSectionsByType("not-existing")).toHaveLength(0);
    expect(content.getSectionsByLabels("not-existing")).toHaveLength(0);

    expect(content.getConceptPropertiesByLabels("Properties")).toHaveLength(1);
    expect(
      content.getTextFragmentsByLabels("Description TextFragment"),
    ).toHaveLength(1);
    expect(content.getSectionsByType("Reference")).toHaveLength(1);
    expect(content.getSectionsByLabels("Description Content")).toHaveLength(1);
  });

  it("Creates ContentModel when sections available", () => {
    const content = new AttributeContent(json);

    const firstSection = content.sections[0];
    expect(firstSection instanceof SectionModel).toBe(true);

    expect(firstSection.selflink.href.toString()).toBe(
      "/content/_Context/Police%20incident%20decisions/Content/Breath%20analysis/Breathalyzer%20test.formalsource/Alert.%3A-",
    );
    expect(firstSection.selfhref.toString()).toBe(
      "/content/_Context/Police%20incident%20decisions/Content/Breath%20analysis/Breathalyzer%20test.formalsource/Alert.%3A-",
    );

    expect(firstSection.type).toBe("Reference");
    expect(firstSection.label).toBe("Alert");
    expect(firstSection.number).toBe("1");
    expect(firstSection.body).toBe(
      "<p>The limit of 0.5 per mille has been exceeded.</p>",
    );
    expect(firstSection.childSectionLinks).toHaveLength(2);
    expect(firstSection.selfContentLink.selfhref.toString()).toBe(
      "/content/_Context/Police%20incident%20decisions/Content/Breath%20analysis/Breathalyzer%20test.formalsource/Alert.%3A-",
    );
    expect(firstSection.selfContentLink.encodedHref.toString()).toBe(
      "/content/_Context%2FPolice%2520incident%2520decisions%2FContent%2FBreath%2520analysis%2FBreathalyzer%2520test.formalsource/Alert.%3A-",
    );

    expect(firstSection.childSections).toHaveLength(2);
    expect(firstSection.subSections).toHaveLength(2);
    expect(firstSection.referenceHash).toBe(4345174491453);
  });

  it("can handle referredSections", () => {
    const content = new AttributeContent(json);

    const referredSections = content.getReferredSections();
    expect(referredSections[0].id).toBe("PassSubSection");
    expect(referredSections[1].id).toBe("PassSubSubSubSection");
    expect(referredSections[2].id).toBe("PassAlert");
  });

  it("can retrieve all content", () => {
    const content = new AttributeContent(json);

    expect(content.elements).toHaveLength(4);
  });
});
