"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchUserBorrow = exports.getDataSelectQuery = exports.searchBookForModalBorrowQuery = exports.searchBookBorrowQuery = exports.getWarehouseIdByEducationalLevelCodeQuery = exports.queryGetPrefixCode = exports.getBookScanReturnQueryV2 = exports.getQrPrefixCodeQuery = exports.getListBookStateQuery = void 0;
const constant_1 = require("../../constant");
const constants_1 = require("./constants");
exports.getListBookStateQuery = `
  query getListBookStateQuery($schoolId: Int!) {
    library_state(
      where: {deleted_at: {_is_null: true}, school_id: {_eq: $schoolId}}
    ) {
      id
      code
    }
  }
`;
exports.getQrPrefixCodeQuery = `
  query getQrPrefixCodeQuery($id: Int!) {
    library_warehouse(where: {id: {_eq: $id}}) {
      qr_code_prefix
      tem_name
    }
  }
`;
exports.getBookScanReturnQueryV2 = `
  query getBookScanReturnQueryV2(
    $code: String!
    $school_id: Int!
    $warehouseId: Int!
    $currentSchoolYearId: Int!
  ) {
    library_dang_ki_ca_biet(
      where: {
        deleted_at: {_is_null: true}
        code: {_eq: $code}
        school_id: {_eq: $school_id}
        warehouse_id: {_eq: $warehouseId}
        library_book_copy: {
          deleted_at: {_is_null: true}, 
          library_book: {
            deleted_at: {_is_null: true}
          }
        }
      }
    ) {
      id
      pre_state
      code
      library_book_copy {
        library_book_images(where: {deleted_at: {_is_null: true}}) {
          url
        }
        library_book {
          title
        }
      }
      state_id
      library_borrow_books(where: {return_date:{_is_null: true} library_borrow_ticket:{library_ticket_state:{code:{_eq: "${constants_1.BORROWED_TICKET_CODE}"}}} deleted_at: {_is_null: true}}) {
        id
        library_borrow_ticket {
          id
          library_borrow_books(where: {deleted_at: {_is_null: true}}) {
            return_date
          }
          actual_borrow_date
          expect_return_date
          user {
            full_name
            user_roles(where: {deleted_at: {_is_null: true}}) {
              role {
                role_code
                name
              }
            }
            profile_staff {
              user_code
            }
            profile_student {
              user_code
            }
            profile_teacher {
              user_code
            }
            classroom_students(
              where: {
                deleted_at: {_is_null: true}
                classroom: {school_year_id: {_eq: $currentSchoolYearId}}
              }
            ) {
              classroom {
                name
              }
            }
          }
        }
      }
    }
  }
`;
exports.queryGetPrefixCode = `
  query queryGetPrefixCode($id: Int!) {
    library_warehouse(where: {id: {_eq: $id}}) {
      qr_code_prefix
    }
  }
`;
exports.getWarehouseIdByEducationalLevelCodeQuery = `
  query getWarehouseIdByEducationalLevelCodeQuery(
    $educationalLevelCode: String!
    $schoolId: Int!
  ) {
    library_warehouse_educational_level(
      where: {educational_level: {school_id: {_eq: $schoolId} code: {_eq: $educationalLevelCode}}}
    ) {
      warehouse_id
    }
  }
`;
exports.searchBookBorrowQuery = `
  query searchBookBorrowQuery(
    $school_id: Int!
    $warehouseId: Int!
    $code: String!
  ) {
    library_dang_ki_ca_biet(
      where: {
        code: {_eq: $code}
        warehouse_id: {_eq: $warehouseId}
        school_id: {_eq: $school_id}
        deleted_at: {_is_null: true}
        library_book_copy: {
          deleted_at: {_is_null: true}
          library_book: {deleted_at: {_is_null: true}}
        }
      }
    ) {
      id
      code
      state_id
      library_state {
        name
        code
      }
      library_book_copy {
        id
        library_book_images(where: {deleted_at: {_is_null: true}}) {
          url
        }
        library_book {
          title
        }
      }
    }
  }
`;
const searchBookForModalBorrowQuery = function (schoolId, warehouseId) {
    return {
        enable: true,
        keyResult: 'library_book_copy',
        query: `
      query searchBookForModalBorrowQuery {
        library_book_copy(where: {
            deleted_at: {_is_null: true}, 
            warehouse_id: {_eq: ${warehouseId}}, 
            school_id: {_eq: ${schoolId}}
            library_book: { 
              _or:[
                {title: { _ilike: "%@value%" }}
                {title_unaccent: { _ilike: "%@value%" }}
              ]
              deleted_at: {_is_null: true},
              is_active: {_eq: ${constants_1.STATUS_BOOK.ACTIVE}}
            }
            }
          ) {
          library_book {
            title
          }
          library_book_images(where: {deleted_at: {_is_null: true}}) {
            url
          }
          library_dang_ki_ca_biets(where: {
            deleted_at: {_is_null: true}, 
            library_state: {
              code:{_in: [
                ${constants_1.IN_WAREHOUSE_STATE_CODE},
                ${constants_1.IN_BOOKSHELF_STATE_CODE}
              ]}
            }
          }) {
            id
            code
            state_id
          }
        }
      }
    `,
    };
};
exports.searchBookForModalBorrowQuery = searchBookForModalBorrowQuery;
const getDataSelectQuery = (educationalLevelCode) => {
    const classroomConditional = educationalLevelCode === constant_1.TIEU_HOC_CODE
        ? `grade: {educational_level: {code: {_eq: ${constant_1.TIEU_HOC_CODE}}}}`
        : `grade: {educational_level: {code: {_neq: ${constant_1.TIEU_HOC_CODE}}}}`;
    return `
    query getDataSelectQuery($school_id: Int!, $school_year_id: Int!, $educationalLevelCode: String!) {
      library_book_borrower(
        where: {deleted_at: {_is_null: true}, school_id: {_eq: $school_id}}
      ) {
        id
        name
        code
        default
      }

      classroom(
        where: {
          deleted_at: {_is_null: true}
          code: {_is_null: false}
          school_id: {_eq: $school_id}
          ${classroomConditional}
          school_year_id:{_eq: $school_year_id}
        }
      ) {
        id
        name
      }

      library_warehouse_educational_level(
        where: {educational_level: {code: {_eq: $educationalLevelCode}}}
      ) {
        warehouse_id
        library_warehouse {
          qr_code_prefix
          tem_name
        }
      }

      ticketStates: library_ticket_state(
        where: {deleted_at: {_is_null: true}, school_id: {_eq: $school_id}}
      ) {
        code
        id
      }
    }
  `;
};
exports.getDataSelectQuery = getDataSelectQuery;
const searchUserBorrow = function (schoolId, currentSchoolYearId, filterProfile) {
    return {
        enable: true,
        keyResult: 'users_search',
        query: `
      query searchUserQuery {
        users_search(
          limit: 20
          args: { q: "%@value%" }
          where: {
            school_id: { _eq: ${schoolId} }
            deleted_at: { _is_null: true }

            user_roles: {
              is_active: { _eq: true }
              deleted_at: { _is_null: true }
              role: {
                deleted_at: { _is_null: true }
              }
            }
            ${filterProfile}
          }
        ) {
          id
          full_name

          user_roles (
            where:{ 
              user: {
              	school_id: {_eq: ${schoolId}}
            	}
              role:{
                school_id: {_eq: ${schoolId}}
                deleted_at: {_is_null: true}
              }
              is_active: {_eq: true}
              deleted_at: {_is_null: true}
            }
          ) {
            role {
              name
              role_code
            }
          }

          profile_staff { user_code }
          profile_student { user_code }
          profile_teacher { user_code }

          library_borrow_tickets(
            where: {
              cancel_date: { _is_null: true }
              deleted_at: { _is_null: true }
              library_ticket_state:{
                code:{_in: [
                  ${constants_1.BORROWED_TICKET_CODE},
                  ${constants_1.AWAIT_COLLECT_TICKET_CODE},
                  ${constants_1.AWAIT_CONFIRM_TICKET_CODE}
                ]}
              }
            }
          ) {
            id
          }

          classroom_students(
            where: {
              deleted_at: { _is_null: true }
              classroom: {
                school_year_id: { _eq: ${currentSchoolYearId} }
                deleted_at: { _is_null: true }
              }
            }
          ) {
            classroom {
              name
              grade { name }
            }
          }
        }
      }
    `,
    };
};
exports.searchUserBorrow = searchUserBorrow;
//# sourceMappingURL=query.js.map